Click here to Skip to main content
14,355,178 members
Rate this:
Please Sign up or sign in to vote.
See more: , +
Dim textControl() As Label 'Array of label objects
Dim txtcnt As Integer


Public Sub addTextControl()
       txtcnt = txtcnt + 1 'Integer keeps track of number of instances created
       ReDim textControl(txtcnt)
       Me.Controls.Add(textControl(txtcnt))
       AddHandler textControl(txtcnt).MouseDown, AddressOf Me.textMouseDown

   End Sub


Private Sub textMouseDown(ByVal tag As String)
    'Return which instance of the object raised the event
    'I need help at this point
   End Sub
Posted
Comments
Sergey Alexandrovich Kryukov 22-Sep-13 12:13pm
   
Not a question. Help with what?
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

Technically, you are supposed to expose us a problem and ask for a formal question.

Anyway, your textMouseDown method has an invalid signature if you want to use it as an eventhandler for MouseDown event:
Private Sub TextMouseDown(sender As Object, e As MouseEventArgs)
   Dim myLabel As Label = CType(sender, Label)
   ' ...
End Sub

Remark: in your addTextControl method, you never create a new Label. I would bet this method throws a NullReferenceException at runtime.
Remark 2: redim an array is a costly operation. Why not using a List(Of Label) instead?
   
v3
Comments
Sergey Alexandrovich Kryukov 22-Sep-13 12:14pm
   
5ed (even though it was not a question :-)
—SA
phil.o 23-Sep-13 1:43am
   
Thanks :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

You need to change the signature of your textMouseDown method to match that of the event you're wiring up.

Something like this might work for you;
Dim textControl() As Label 'Array of label objects
Dim txtcnt As Integer

Public Sub addTextControl()
    txtcnt = txtcnt + 1 'Integer keeps track of number of instances created
    ReDim textControl(txtcnt)
    Me.Controls.Add(textControl(txtcnt))
    AddHandler textControl(txtcnt).MouseDown, AddressOf Me.textMouseDown

End Sub

' This method needs this signature, it's not allowed to use just any signature
Private Sub textMouseDown(sender As Object, args As MouseEventArgs)

    ' The actual Label
    Dim currentLabel As Label = CType(sender, Label)

    ' The index of the label in your textControl array
    Dim currentIndex As Integer = textControl.ToList().IndexOf(currentLabel)

    ' Tag of it
    Dim tag As String = currentLabel.Tag
End Sub

Hope this helps,
Fredrik
   
Comments
Sergey Alexandrovich Kryukov 22-Sep-13 12:14pm
   
5ed.
—SA
asjadathick 22-Sep-13 19:11pm
   
Thanks! This was exactly what I was looking for.
Rate this:
Please Sign up or sign in to vote.

Solution 3

See this small example for Event Handling VB.net :
Private _objLabel As Label
Private _ControlId As Integer = 0

Public Sub AddLabelControl()
     _objLabel = New Label         'Create New Object Of Label Control
     _ControlId = _ControlId + 1   'Increase Control Id Count By 1
     _objLabel.Name = "Label" & _ControlId 'Control Name
     _objLabel.Text = "Label" & _ControlId 'Control Text To Display
     _objLabel.Location = New Point(10, 20 * _ControlId) ' Control Location
      AddHandler _objLabel.MouseDown, AddressOf _objLabelMouseDown 'Adding Event Handler
      Me.Controls.Add(_objLabel) 'Adding Control On Form
End Sub

Private Sub _objLabelMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
       MessageBox.Show(CType(sender, Label).Name) 'Display Control Name On Mouse Down
End Sub

To Test this Code Write following Code on Form_Load
While _ControlId < 5
       AddLabelControl() 'Add Five Label's On Form
End While

I hope this will help you. :)
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100