0

当我按下提交按钮时,我将控件的背景颜色设置为红色,而必填字段中没有任何数据。但我需要在这些缺失的必填字段中重新输入数据。当我开始重新输入数据时,背景红色变为白色。是否有机会将所有其他必填字段颜色更改为白色?

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

这是我的代码

4

1 回答 1

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

因此,在您发表评论后2:

    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
于 2013-01-09T15:19:05.323 回答