Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB10

Multiple Event Handling for the Lazy Ones

1.00/5 (1 vote)
24 May 2011CPOL 11.3K  
Handle code redundancy for many controls in one subroutine
When you want to replace all spaces entered in Textboxes, say txtH1,txtH2,txtH3,txtH4, instead of writing separate event handlers like :
VB
 Private Sub txtH1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtH1.TextChanged
'Code
End Sub

VB
 Private Sub txtH2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtH2.TextChanged
'Code
End Sub

and so on,

VB
 Private Sub txtH1to7TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtH1.TextChanged,txtH2.TextChanged, txtH3.TextChanged,txtH4.TextChanged
'Code
End Sub


would do the job.

This can be extended to the same event of any number of the same type of control.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)