在一个处理程序中处理文本框事件

时间:2018-12-26 08:38:44

标签: vb.net

我确实知道如何处理表单中文本框的事件。但是要使这段代码更短。让我每次在处理程序中停止每次txtDraw1,2,3,4,5的编写,并且通常将txtDraw的值设置为1到8。即,在包含每个txtDraw的句柄中,而无需手动编写它们。

Private Sub TextBoxes_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtDrawA1.TextChanged, txtDrawA2.TextChanged, txtDrawA3.TextChanged, txtDrawA4.TextChanged, txtDrawA5.TextChanged, txtDrawA6.TextChanged, txtDrawA7.TextChanged, txtDrawA8.TextChanged, txtDrawB1.TextChanged, txtDrawB2.TextChanged, txtDrawB3.TextChanged, txtDrawB4.TextChanged, txtDrawB5.TextChanged, txtDrawB6.TextChanged, txtDrawB7.TextChanged, txtDrawB8.TextChanged, txtDrawC1.TextChanged, txtDrawC2.TextChanged, txtDrawC3.TextChanged, txtDrawC4.TextChanged, txtDrawC5.TextChanged, txtDrawC6.TextChanged, txtDrawC7.TextChanged, txtDrawC8.TextChanged, txtDrawD1.TextChanged, txtDrawD2.TextChanged, txtDrawD3.TextChanged, txtDrawD4.TextChanged, txtDrawD5.TextChanged, txtDrawD6.TextChanged, txtDrawD7.TextChanged, txtDrawD8.TextChanged, txtDrawE1.TextChanged, txtDrawE2.TextChanged, txtDrawE3.TextChanged, txtDrawE4.TextChanged, txtDrawE5.TextChanged, txtDrawE6.TextChanged, txtDrawE7.TextChanged, txtDrawE8.TextChanged, txtDrawF1.TextChanged, txtDrawF2.TextChanged, txtDrawF3.TextChanged, txtDrawF4.TextChanged, txtDrawF5.TextChanged, txtDrawF6.TextChanged, txtDrawF7.TextChanged, txtDrawF8.TextChanged, txtDrawG1.TextChanged, txtDrawG2.TextChanged, txtDrawG3.TextChanged, txtDrawG4.TextChanged, txtDrawG5.TextChanged, txtDrawG6.TextChanged, txtDrawG6.TextChanged, txtDrawG7.TextChanged, txtDrawG8.TextChanged, txtDrawH1.TextChanged, txtDrawH2.TextChanged, txtDrawH3.TextChanged, txtDrawH4.TextChanged, txtDrawH5.TextChanged, txtDrawH6.TextChanged, txtDrawH7.TextChanged, txtDrawH8.TextChanged, txtDrawI1.TextChanged, txtDrawI2.TextChanged, txtDrawI3.TextChanged, txtDrawI4.TextChanged, txtDrawI5.TextChanged, txtDrawI6.TextChanged, txtDrawI7.TextChanged, txtDrawI8.TextChanged
    SetTextBoxColor(DirectCast(sender, TextBox))
End Sub
Sub SetTextBoxColor(ByVal txt As TextBox)
    Select Case txt.Text
        Case "1"
            txt.BackColor = Color.DarkSalmon
        Case "2"
            txt.BackColor = Color.Aqua
        Case "3"
            txt.BackColor = Color.DimGray
        Case "4"
            txt.BackColor = Color.DarkBlue
        Case "5"
            txt.BackColor = Color.Violet
        Case "6"
            txt.BackColor = Color.BlueViolet
        Case "7"
            txt.BackColor = Color.Yellow
    End Select
End Sub

1 个答案:

答案 0 :(得分:1)

首先,您不必手动将其写出。您可以在设计器中选择多个控件,打开“属性”窗口,单击“事件”按钮,然后双击一个事件以为所有选定控件的该事件生成一个事件处理程序。您还可以使用事件的下拉菜单选择现有的处理程序,以将一个或多个控件添加到其的Handles子句中。

有两种方法可以在VB中注册事件处理程序。您可以使用WithEventsHandles来完成操作,在这种情况下,必须在代码中包含每个标识符。这意味着您不能使代码更短并使用Handles子句。 Handles子句的替代方法是使用AddHandlerRemoveHandler。通常,这是在运行时而不是设计时创建的控件来完成的,但是您可以为两者之一做。就您而言,您可以将以下代码添加到以下形式的Load事件处理程序中:

Dim letters = {"A", "B", "C", "D", "E", "F", "H", "I"}

For Each letter In letters
    For n = 1 To 8
        AddHandler Controls($"txtDraw{letter}{n}").TextChanged, AddressOf TextBoxes_TextChanged
    Next
Next

您将在FormClosed事件处理程序中执行相同的操作,但是使用RemoveHandler而不是AddHandler