Detect Label Click

WebKill

Member
Joined
Apr 7, 2008
Messages
12
Programming Experience
Beginner
Is there a way to detect when a label is clicked without doing on click events for all the labels? I want to be able to detect the click, find what has been clicked, then I can change the label since I'll know which one has been clicked.
 
You could make one general ClickEvent, add only those labels that you want to be handled by it to the Handles clause......

As in this case I might have 9 Labels on a form but on 3 will have the Click event handle by this method.
VB.NET:
Public Sub LabelClickEvent(byval sender as object, byval e as EventArgs) Handles label1.Click, label4.Click, label9.Click

     Select Case DirectCast(sender, Label).Name.ToUpper()
      'Process for different label names
            Case "LABEL1":
      ' Alternatively you could use the tag to store information and then process based on that in the select
      End Select
End Sub
 
And you can give them all this click event from the form load event with this loop:
VB.NET:
For Each ctr As Control In Me.Controls
   If TypeOf ctr Is Label Then
   Dim lb As Label = CType(ctr, Label)
   Addhandler lb.Click, Addressof LabelClickEvent
Next ctr
Then use the sub the @demausdauth posted, and no need for the Handles clause.
 
Is it possible to have it handle all labels or all controls? I want to avoid coding the specific names and instead let the function detect what it is.
 
VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		For Each ctrl As Control In Me.Controls
			If TypeOf ctrl Is Label Then
				AddHandler ctrl.Click, AddressOf label_Click
			End If
		Next
	End Sub

	Private Sub label_Click(ByVal sender As Object, ByVal e As EventArgs)
		MessageBox.Show(CType(sender, Label).Name & " clicked")
	End Sub

Edit: Oops, looks like newguy beat me to the punch.
 
Loop the form's controls collection to get all of them:
VB.NET:
For Each lbl As Control In Me.Controls
  If TypeOf lbl Is Label Then AddHandler(yourSubRoutine, Ctype(lbl, Label))
Next

Edit: NewGuy got to it before me as well
 
Is it possible to have it handle all labels or all controls? I want to avoid coding the specific names and instead let the function detect what it is.

You can either do all labels or all controls.

VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		For Each ctrl As Control In Me.Controls
			AddHandler ctrl.Click, AddressOf ctrl_Click
		Next
	End Sub

	Private Sub ctrl_Click(ByVal sender As Object, ByVal e As EventArgs)
		MessageBox.Show(String.Format("Type of control: {0}{1}Name of control: {2}", sender.GetType, Environment.NewLine, sender.Name))
	End Sub
 
Thanks all, newguy got to it before my second post as well, lol. Thanks newguy! One more question though, I have some labels in a Panel, and this does not seem to work for those labels, any ideas?
 
Rather than adding a specific loop for the panel control you may want to check if the control has children and then recursively loop through that ControlCollection to add the Click handler.

VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		AddClickHandlers(Me.Controls)
	End Sub

	Private Sub AddClickHandlers(ByVal ctrlColl As System.Windows.Forms.Form.ControlCollection)
		For Each ctrl As Control In ctrlColl
			AddHandler ctrl.Click, AddressOf ctrl_Click
			If ctrl.HasChildren Then
				AddClickHandlers(ctrl.Controls)
			End If
		Next
	End Sub

	Private Sub AddClickHandlers(ByVal ctrlColl As System.Windows.Forms.Control.ControlCollection)
		For Each ctrl As Control In ctrlColl
			AddHandler ctrl.Click, AddressOf ctrl_Click
			If ctrl.HasChildren Then
				AddClickHandlers(ctrl.Controls)
			End If
		Next
	End Sub

	Private Sub ctrl_Click(ByVal sender As Object, ByVal e As EventArgs)
		MessageBox.Show(String.Format("Type of control: {0}{1}Name of control: {2}", sender.GetType, Environment.NewLine, sender.Name))
	End Sub
 
It may be easier to just select all the controls in question in Designer and select the common event handler for them there in Properties window (Events view).
 
Back
Top