Click here to Skip to main content
16,016,962 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
VB
Dim textControl() As Label 'Array of label objects
Dim txtcnt As Integer


VB
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


VB
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

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:
VB
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?
 
Share this answer
 
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 :)
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;
VB
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
 
Share this answer
 
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.
See this small example for Event Handling VB.net :
VB
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
VB
While _ControlId < 5
       AddLabelControl() 'Add Five Label's On Form
End While

I hope this will help you. :)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900