简体   繁体   中英

When any of the textboxes changes (VB)

I'm a beginner to VB and I'm looking to optimize my code a little. I have 9 textboxes, and every time one of them changes I run a Subroutine based on it's content. Is there a way to make it, perhaps with a for loop, go through all of the 9 textboxes and register when any of them changes. Here's ..my code looks like at the moment...

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

..etc

You don't need a separate handler for each text box. A single handler method can handle all of them. If you need a value for each text box to pass into your checkInput method, just use the Tag property of each text box.

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

Try multiple Handles:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM