任何文本框更改时(VB)

时间:2015-01-27 20:37:20

标签: vb.net

我是VB的初学者,我正在寻求优化我的代码。我有9个文本框,每当其中一个更改时,我会根据它的内容运行子程序。是否有一种方法可以使用for循环,遍历所有9个文本框并在其中任何一个更改时进行注册。这是..我的代码现在看起来像......

Private Sub tbBox1_TextChanged(sender As Object, e As EventArgs) Handles tbBox1.TextChanged
    checkInput(tbBox1, 0, 0)
End Sub

Private Sub tbBox2_TextChanged(sender As Object, e As EventArgs) Handles tbBox2.TextChanged
    checkInput(tbBox2, 0, 1)
End Sub

Private Sub tbBox3_TextChanged(sender As Object, e As EventArgs) Handles tbBox3.TextChanged
    checkInput(tbBox3, 0, 2)
End Sub

...等

2 个答案:

答案 0 :(得分:3)

每个文本框都不需要单独的处理程序。单个处理程序方法可以处理所有这些。如果您需要将每个文本框的值传递到checkInput方法,只需使用每个文本框的Tag属性。

Private Sub TextBoxChanged(sender As Object, e As EventArgs) Handles tbBox1.TextChanged, tbBox2.TextChanged, tbBox3.TextChanged 'etc.
    checkInput(sender, 0, sender.Tag)
End Sub

答案 1 :(得分:1)

尝试多个句柄:

Private Sub tbBox_TextChanged(sender As Object, e As EventArgs) Handles _
                              tbBox1.TextChanged, _
                              tbBox2.TextChanged, _
                              tbBox3.TextChanged
  checkInput(sender, 0, 0)
End Sub