[VB.NET]
Private Sub ComboBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox2.Validating, ComboBox1.Validating, ComboBox3.Validating, ComboBox4.Validating
'MsgBox(e.ToString)
'MsgBox(sender.ToString)
'Exit Sub
'避免視窗關閉時(引發驗證事件)
If frmClose = True Then
Exit Sub
End If
'表示都沒有更動, 所以也就不需要再做下面的判斷 -> 新值 = 舊值
If Me._btnOperation = "update" AndAlso ComboBox1.Text.Trim = Trim(ComboBox1.Tag) AndAlso _
ComboBox2.Text.Trim = Trim(ComboBox2.Tag) AndAlso _
ComboBox3.Text.Trim = Trim(ComboBox3.Tag) AndAlso _
ComboBox4.Text.Trim = Trim(ComboBox4.Tag) Then
Exit Sub
End If
'MsgBox(e.ToString)
'取得目前焦點位置....
'MsgBox(CType(sender, Control).Name)
'Exit Sub
If (CType(sender, Control).Name = "ComboBox2" OrElse CType(sender, Control).Name = "ComboBox3") _
AndAlso Me._btnOperation = "update" AndAlso Me.DataGridView1.Rows.Count > 0 _
AndAlso (ComboBox2.Text.Trim <> Trim(ComboBox2.Tag) OrElse _
ComboBox3.Text.Trim <> Trim(ComboBox3.Tag)) Then
MsgBox("有單據明細資料不能修改此欄位, 請重新輸入!")
ComboBox2.Text = ComboBox2.Tag
ComboBox3.Text = ComboBox3.Tag
Exit Sub
End If
If _Editing = True Then
If String.IsNullOrEmpty(Me.ComboBox2.Text) Then
MsgBox("材質編號空白, 請重新輸入!")
ErrorProvider1.SetError(Me.ComboBox2, "材質編號空白, 請重新輸入!")
Me.ComboBox2.Focus()
Exit Sub
ElseIf Me.ComboBox2.SelectedIndex = -1 Then
MsgBox("材質編號不允許自行輸入, 請重新選擇!")
ErrorProvider1.SetError(Me.ComboBox2, "材質編號不允許自行輸入, 請重新選擇!")
Me.ComboBox2.Focus()
Exit Sub
End If
If String.IsNullOrEmpty(Me.ComboBox3.Text) Then
MsgBox("牌價編號空白, 請重新輸入!")
ErrorProvider1.SetError(Me.ComboBox3, "牌價編號空白, 請重新輸入!")
Me.ComboBox3.Focus()
Exit Sub
ElseIf Me.ComboBox3.SelectedIndex = -1 Then
MsgBox("牌價編號不允許自行輸入, 請重新選擇!")
ErrorProvider1.SetError(Me.ComboBox3, "牌價編號不允許自行輸入, 請重新選擇!")
Me.ComboBox3.Focus()
Exit Sub
End If
If String.IsNullOrEmpty(Me.ComboBox1.Text) Then
MsgBox("業務員編號空白, 請重新輸入!")
ErrorProvider1.SetError(Me.ComboBox1, "業務員編號空白, 請重新輸入!")
Me.ComboBox1.Focus()
Exit Sub
ElseIf Me.ComboBox1.SelectedIndex = -1 Then
MsgBox("業務員編號不允許自行輸入, 請重新選擇!")
ErrorProvider1.SetError(Me.ComboBox1, "業務員編號不允許自行輸入, 請重新選擇!")
Me.ComboBox1.Focus()
Exit Sub
End If
If String.IsNullOrEmpty(Me.ComboBox4.Text) Then
MsgBox("幣別欄位空白, 請重新輸入!")
ErrorProvider1.SetError(Me.ComboBox4, "幣別欄位空白, 請重新輸入!")
Me.ComboBox4.Focus()
Exit Sub
ElseIf Me.ComboBox4.SelectedIndex = -1 Then
MsgBox("幣別欄位不允許自行輸入, 請重新選擇!")
ErrorProvider1.SetError(Me.ComboBox4, "幣別欄位不允許自行輸入, 請重新選擇!")
Me.ComboBox4.Focus()
Exit Sub
End If
'取消事件
'e.Cancel = True
End If
ErrorProvider1.SetError(Me.ComboBox1, "")
ErrorProvider1.SetError(Me.ComboBox2, "")
ErrorProvider1.SetError(Me.ComboBox3, "")
ErrorProvider1.SetError(Me.ComboBox4, "")
End Sub
[C#.NET]
private void txt_訂購單號_Validating(object sender, CancelEventArgs e)
{
if (this._Editing==false)
return;
//MessageBox.Show(typeof((Control)sender));
//MessageBox.Show((sender).GetType().ToString());
//MessageBox.Show(((Control)sender).GetType().ToString());
//MessageBox.Show(this.ActiveControl.Name);
//switch (sender.GetType().ToString())
//{
// case "TextBox":
// MessageBox.Show("AA");
// break;
//}
Regex rx;
if (sender is TextBox)
{
//if (((TextBox)sender).Name == "")
//{
//}
switch (((TextBox)sender).Name)
{
case "txt_訂購單號":
rx = new Regex(@"^[\S]{1,15}$");
if (!rx.IsMatch(txt_訂購單號.Text.Trim()))
{
MessageBox.Show(
this,
"訂購單號:不可空白, 長度不可超過15!!",
"✚格式化錯誤✚",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
errorProvider1.SetError(txt_訂購單號, "不可空白, 長度不可超過15!!");
//txt_訂購單號.Focus();
e.Cancel = true; //按右上角FormClose...會被取消
}
break;
case "txt_AD":
rx = new Regex(@"^(25[0-5]|2[0-4]\d|[1]\d{2}|[0-9]{0,2})$");
if (!rx.IsMatch(txt_AD.Text.Trim()))
{
MessageBox.Show(
this,
"AD:請輸入數值0-255!!",
"✚格式化錯誤✚",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
errorProvider1.SetError(txt_AD, "請輸入數值0-255!!");
//txt_AD.Focus();
e.Cancel = true; //按右上角FormClose...會被取消
}
break;
case "txt_註記":
rx = new Regex(@"^[\S]{0,50}$");
if (!rx.IsMatch(txt_註記.Text.Trim()))
{
MessageBox.Show(
this,
"註記:長度不可超過50!!",
"✚格式化錯誤✚",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
errorProvider1.SetError(txt_註記, "長度不可超過50!!");
//txt_註記.Focus();
e.Cancel = true; //按右上角FormClose...會被取消
}
break;
case "txt_客戶訂號":
rx = new Regex(@"^[\S]{0,20}$");
if (!rx.IsMatch(txt_客戶訂號.Text.Trim()))
{
MessageBox.Show(
this,
"註記:長度不可超過20!!",
"✚格式化錯誤✚",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
errorProvider1.SetError(txt_客戶訂號, "長度不可超過20!!");
//txt_客戶訂號.Focus();
e.Cancel = true; //按右上角FormClose...會被取消
}
break;
case "txt_詢單編號":
rx = new Regex(@"^[\S]{0,15}$");
if (!rx.IsMatch(txt_詢單編號.Text.Trim()))
{
MessageBox.Show(
this,
"詢單編號:長度不可超過15!!",
"✚格式化錯誤✚",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
errorProvider1.SetError(txt_詢單編號, "長度不可超過15!!");
//txt_客戶訂號.Focus();
e.Cancel = true; //按右上角FormClose...會被取消
}
break;
case "txt_交易條件":
case "txt_付款方式":
rx = new Regex(@"^[\S]{0,50}$");
if (!rx.IsMatch(((TextBox)sender).Text.Trim()))
{
MessageBox.Show(
this,
((TextBox)sender).Tag + ":長度不可超過50!!",
"✚格式化錯誤✚",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
errorProvider1.SetError(((TextBox)sender), "長度不可超過50!!");
//txt_客戶訂號.Focus();
e.Cancel = true; //按右上角FormClose...會被取消
}
break;
case "txt_訂單金額":
//rx = new Regex(@"^(25[0-5]|2[0-4]\d|[1]\d{2}|[0-9]{0,2})$");
//if (!rx.IsMatch(txt_訂單金額.Text.Trim()))
//{
//}
decimal decResult = 0;
bool decFalg = decimal.TryParse(txt_訂單金額.Text.Trim(), out decResult);
if ( !decFalg || decResult < 0)
{
MessageBox.Show(
this,
"金額:請輸入正整數!!",
"✚格式化錯誤✚",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
errorProvider1.SetError(txt_訂單金額, "請輸入正整數!!");
//txt_AD.Focus();
e.Cancel = true; //按右上角FormClose...會被取消
}
break;
case "txt_數量":
//rx = new Regex(@"^(25[0-5]|2[0-4]\d|[1]\d{2}|[0-9]{0,2})$");
//if (!rx.IsMatch(txt_數量.Text.Trim()))
//{
//}
int intResult = 0;
bool intFalg = int.TryParse(txt_數量.Text.Trim(), out intResult);
if ( !intFalg || intResult < 0)
{
MessageBox.Show(
this,
"數量:請輸入正整數!!",
"✚格式化錯誤✚",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
errorProvider1.SetError(txt_數量, "請輸入正整數!!");
//txt_AD.Focus();
e.Cancel = true; //按右上角FormClose...會被取消
}
break;
default:
break;
}
}
else if (sender is ComboBox)
{
switch (((ComboBox)sender).Name)
{
case "cmb_客戶編號":
case "cmb_業務編號":
case "cmb_抬頭編號":
case "cmb_流程編號":
case "cmb_幣別編號":
case "cmb_銀行編號":
//rx = new Regex(@"^[\S]{0,20}$");
//if (!rx.IsMatch(txt_客戶訂號.Text.Trim()))
//}
if(((ComboBox)sender).SelectedIndex == -1)
{
MessageBox.Show(
this,
((ComboBox)sender).Tag + ":請從項目中選擇!!",
"✚格式化錯誤✚",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
errorProvider1.SetError((Control)sender, "請從項目中選擇!!");
//cmb_客戶編號.Focus();
e.Cancel = true; //按右上角FormClose...會被取消
}
break;
default:
break;
}
}
}
private void txt_訂購單號_Validated(object sender, EventArgs e)
{
//MessageBox.Show(sender.ToString());
errorProvider1.SetError((Control)sender, "");
//errorProvider1.SetError(txt_訂購單號, "");
//errorProvider1.SetError(txt_AD, "");
//errorProvider1.SetError(txt_註記, "");
//errorProvider1.SetError(cmb_客戶編號, "");
//errorProvider1.SetError(cmb_業務編號, "");
//errorProvider1.SetError(txt_客戶訂號, "");
}
參考:
Control.Validating 事件
Control.GotFocus 事件
判斷焦點位置
[C#.NET][VB.NET] 如何設定 控制項陣列 / 動態加入控制項
[C#] 動態 button 和事件
C#動態控制項陣列化
沒有留言:
張貼留言