2013-01-09 84 views
0

I當按下提交按鈕而沒有強制字段中的任何數據時,將控件背景色設置爲紅色。但我需要在這些缺少的必填字段中輸入數據。當我開始輸入數據時,背景紅色變爲白色。是否有機會將所有其他必填字段顏色更改爲白色?vb.net上的控件組按鈕事件

Private Sub TXTEMPID_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TXTEMPID.KeyPress 
    UncheckMyControls() 
    If Asc(e.KeyChar) <> 8 Then 
     If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then 
      e.Handled = True 
     End If 
    End If 
End Sub 

Private Sub UncheckMyControls() 
    Dim txt, cmb, mtxt, rtxt As Control 
    For Each cmb In EMPGBDATA.Controls 
     If TypeOf cmb Is ComboBox Then 
      If cmb.BackColor = Color.Red Then 
       cmb.BackColor = Color.White 
      End If 
     End If 
    Next 
    For Each rtxt In EMPGBDATA.Controls 
     If TypeOf rtxt Is RichTextBox Then 
      If rtxt.BackColor = Color.Red Then 
       rtxt.BackColor = Color.White 
      End If 
     End If 
    Next 
    For Each mtxt In EMPGBDATA.Controls 
     If TypeOf mtxt Is MaskedTextBox Then 
      If mtxt.BackColor = Color.Red Then 
       mtxt.BackColor = Color.White 
      End If 
     End If 
    Next 
    For Each txt In EMPGBDATA.Controls 
     If TypeOf txt Is TextBox Then 
      If txt.BackColor = Color.Red Then 
       txt.BackColor = Color.White 
      End If 
     End If 
    Next 
End Sub 

這是我的代碼

+0

你能告訴我們用你的代碼來更好地理解這個概念和問題? – Nianios

+0

@HaBouF - 我的代碼如下: – Thanzeem

+0

我看不到它。請在問題部分添加代碼。 – Nianios

回答

0

由於SolarBear建議:

 For Each cnt In EMPGBDATA.Controls 
     If cnt.BackColor = Color.Red Then 
      cnt.BackColor = Color.White 
     End If 
     Next 

現在你能準確地描述你的問題?

所以在您的意見: 你說你可以檢查是否必填字段是空白,如果是的話,你將他們的背景顏色設置爲紅色。並彈出消息。 您在第一時間爲用戶嘗試鍵入任何你的文本框的東西顏色重置爲白色所需信息後:

Private Sub txt_KeyPress(sender As System.Object, e As System.EventArgs) Handles TextBox1.KeyPress,TextBox2.KeyPress,TextBox3.KeyPress,TextBox4.KeyPress 
'After the Handles you put all the textboxes,richtexboxes that you want 
'THis way you handle the same event for a lot of controls in the same Sub 
    UncheckMyControls() 
    End Sub 

您comments2所以經過:

Private Sub control_KeyPress(sender As System.Object, e As System.EventArgs) Handles TextBox1.KeyPress,TextBox2.KeyPress,TextBox3.KeyPress,ComboBox1.KeyPress,ComboBox2.KeyPress 
'After the Handles you put all the textboxes,richtexboxes,comboboxes that you want 
'THis way you handle the same event for a lot of controls in the same Sub 
    UncheckMyControls() 
    End Sub 
+0

是的.. Itz工作的人....感謝您的支持.... – Thanzeem

+0

但我再次想念一些領域進入。它顯示紅色,而我按下提交按鈕。我再次開始輸入數據不工作。我的意思是它只在TXTEMPID_KeyPress事件工作..什麼是解決方案? – Thanzeem

+0

@ HaBouF--請檢查問題,並給我一個解決方案.... – Thanzeem