954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Link Label Click Event

Hi guys,
I have a total of 72 link labels on a form and I want them to do a similar task.
Is there a way to accomplishing this without writing each labels individual code?

Example function of the link label

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Listbox1.Items.Add(Label1.Text)
variable = label1.text
    End Sub

 Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
Listbox1.Items.Add(Label2.Text)
variable = label2.text
    End Sub
djjavo
Light Poster
28 posts since Jun 2011
Reputation Points: 10
Solved Threads: 1
 

As the System.Windows.Forms.LinkLabelLinkClickedEventArgs contains the LinkLabel.Link you can use it to do your work. Yu'll need only one Sub like:

Private Sub LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) 
    Listbox1.Items.Add(e.Link.Text)
    variable = e.Link.Text
End Sub


Then, on the click event of each link label defined, change it to point to this sub.

Hope this helps.

lolafuertes
Master Poster
793 posts since Oct 2008
Reputation Points: 120
Solved Threads: 166
 

You could also define a click event for just one of the controls in the set then use AddHandler on form load to add the other controls to that handler. It would save a lot of clicking. You would have to modify the original handler to distinguish between the controls. Or, if you don't mind some cut and paste you could add them as follows:

Private Sub lbl01_Click(sender As System.Object, e As System.EventArgs) Handles _
            lbl01.Click,lbl02.Click,lbl03.Click,lbl04.Click,...

Just extend the line for as many controls as you have. This gets a little messy, however, if you have a lot of controls. I recommend the AddHandler method.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

Thank you both for your responses, unfortunatley I do not understand what you mean. I have tried both code suggestions but none seem to work as intended. Sorry for the hassle.

djjavo
Light Poster
28 posts since Jun 2011
Reputation Points: 10
Solved Threads: 1
 

Any error? what is the expected result? can you pot something else that helps us to help you?

lolafuertes
Master Poster
793 posts since Oct 2008
Reputation Points: 120
Solved Threads: 166
 

Try this. I created a form with five labels and one listbox.

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        AddHandler Label1.Click, AddressOf Label_Click
        AddHandler Label2.Click, AddressOf Label_Click
        AddHandler Label3.Click, AddressOf Label_Click
        AddHandler Label4.Click, AddressOf Label_Click
        AddHandler Label5.Click, AddressOf Label_Click

    End Sub

    Private Sub Label_Click(sender As System.Object, e As System.EventArgs)

        Dim label As Label = sender
        ListBox1.Items.Add(label.Text)

    End Sub

End Class
Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

That is brilliant. Thank you for your help!! :) +1

djjavo
Light Poster
28 posts since Jun 2011
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: