Question Referencing all items on form with Handles keyword

TartanRug

Member
Joined
Sep 14, 2011
Messages
7
Programming Experience
5-10
Hi,

I have a form in VB with many (over 100 - fields, labels, the form itself etc...) items on. I wondered if there was a way of refering to all the items on the form without having to list them all individually in the Handles keyword, which is extremely long winded! I want to carry out the same action wherever the user clicks on the form. At present I have the following code:

Private Sub myform_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtX.Click, _
txtY.Click, txtOS.Click, GroupBox1.Click, opb10.Click, opb100.Click, opb1000.Click, _
Label1.Click, Label2.Click, Label3.Click, Me.Click etc, etc.............

Any help much appreciated!

Simon
 
maybe you could use somthing simler to this, ofcourse you will need to modify it yo your needs.

Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cControl As Control
Dim strString As String = "abc" ' Changes All Button TEXT
For Each cControl In Me.Controls
If (TypeOf cControl Is Button) Then
cControl.Text = "abc" ' Changes All Button TEXT
ElseIf (TypeOf cControl Is TextBox) Then
cControl.Text = "abc" ' Changes All TextBox TEXT
End If

Next cControl
End Sub
 
Last edited:
You could loop through all of the controls on the form, and use AddHandler for the control's click event.

Something like this:

VB.NET:
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
[COLOR=#222222]    Dim oControl As Control

    For Each oControl In Me.Controls
      AddHandler oControl.Click, AddressOf ClickEventHandler
    Next
  End Sub
  Private Sub ClickEventHandler(sender As Object, e As EventArgs)
    ' Your code here to handle the click event for all of the controls
  End Sub[/COLOR]



 
Back
Top