|
-
Oct 20th, 2009, 10:43 AM
#1
Thread Starter
Junior Member
Event Handler For Dynamic Controls
Below I create an array of labels. I would like to add an click event to my labels. Can someone point me in a direction?
Thanks
Code:
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lblQty(196) As Label
Dim x As Integer = 115
Dim y As Integer = 30
For intLoopRows As Integer = 0 To 27
y += 37
For intLoopCols As Integer = 0 To 6
x += 50
lblQty(intLoopRows) = New Label
lblQty(intLoopRows).Text = "0"
lblQty(intLoopRows).SetBounds(x, y, 12, 12)
Me.Controls.Add(lblQty(intLoopRows))
Next
x = 115
Next
End Sub
End Class
-
Oct 20th, 2009, 10:52 AM
#2
Re: Event Handler For Dynamic Controls
Have a search for AddHandler.
Last edited by dee-u; Oct 20th, 2009 at 10:55 AM.
-
Oct 20th, 2009, 11:19 AM
#3
Thread Starter
Junior Member
Re: Event Handler For Dynamic Controls
I did. Thanks. I changed my code (see below) and it does not work.....I am probably missing something very obvious to you more experienced programmers. Thanks for any help.
Code:
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lblQty(196) As Label
For intCreateArray As Integer = 0 To 196
lblQty(intCreateArray) = New Label
AddHandler lblQty(intCreateArray).Click, AddressOf labelclicked
Next
Dim x As Integer = 115
Dim y As Integer = 30
For intLoopRows As Integer = 0 To 27
y += 37
For intLoopCols As Integer = 0 To 6
x += 50
lblQty(intLoopRows) = New Label
lblQty(intLoopRows).Text = "0"
lblQty(intLoopRows).SetBounds(x, y, 12, 12)
Me.Controls.Add(lblQty(intLoopRows))
Next
x = 115
Next
End Sub
Private Sub labelclicked(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Text = "99"
End Sub
End Class
-
Oct 20th, 2009, 12:53 PM
#4
Thread Starter
Junior Member
Re: Event Handler For Dynamic Controls
Still trying with no luck.....please help if you can.
-
Oct 20th, 2009, 01:16 PM
#5
Re: Event Handler For Dynamic Controls
You need to put the addhandler in your inner loop.
VB.Net Code:
For intLoopCols As Integer = 0 To 6
x += 50
lblQty(intLoopRows) = New Label
lblQty(intLoopRows).Text = "0"
lblQty(intLoopRows).SetBounds(x, y, 12, 12)
AddHandler lblQty(intLoopRows).Click, AddressOf labelclicked
Me.Controls.Add(lblQty(intLoopRows))
Next
-
Oct 21st, 2009, 07:28 AM
#6
Thread Starter
Junior Member
Re: Event Handler For Dynamic Controls
dee-U,
Thank you. Is there a way for me to know which lbl was clicked in my event handler?
Thanks
-
Oct 21st, 2009, 07:31 AM
#7
Re: Event Handler For Dynamic Controls
Use the Sender object that is passed in to the event handler
-
Oct 21st, 2009, 07:32 AM
#8
Re: Event Handler For Dynamic Controls
 Originally Posted by bgreer5050
dee-U,
Thank you. Is there a way for me to know which lbl was clicked in my event handler?
Thanks
As is the case with all event handlers, the 'sender' parameter is a reference to the object that raised the event.
-
Oct 21st, 2009, 08:09 AM
#9
Thread Starter
Junior Member
Re: Event Handler For Dynamic Controls
Ok. The labels on the form are an array of labels. How do I know which one was clicked?
messagebox.show(sender.???????)
Thanks Again
-
Oct 21st, 2009, 08:13 AM
#10
Re: Event Handler For Dynamic Controls
Well because Sender is of type Object, you need to cast it to the relevant type first before you can access properties on it. Something like this:
vb Code:
MessageBox.Show(DirectCast(Sender, Control).Name)
or, if you know it will ALWAYS be a label that is the sender:
vb Code:
MessageBox.Show(DirectCast(Sender, Label).Name)
-
Oct 21st, 2009, 10:34 AM
#11
Thread Starter
Junior Member
Re: Event Handler For Dynamic Controls
I am still missing something. I am getting a blank messagebox.
Here is the code as it stands now:
Code:
Public Class frmMain
Dim lblQty(196) As Label
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Protected Friend Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dim lblQty(196) As Label
For intCreateArray As Integer = 0 To 196
lblQty(intCreateArray) = New Label
Next
Dim x As Integer = 115
Dim y As Integer = 30
For intLoopRows As Integer = 0 To 27
y += 37
For intLoopCols As Integer = 0 To 6
x += 50
lblQty(intLoopRows) = New Label
'AddHandler lblQty(intLoopRows).Click, AddressOf labelclicked
lblQty(intLoopRows).Text = "0"
lblQty(intLoopRows).SetBounds(x, y, 12, 12)
Me.Controls.Add(lblQty(intLoopRows))
AddHandler lblQty(intLoopRows).Click, AddressOf labelclicked
Next
x = 115
Next
End Sub
Private Sub labelclicked(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show((DirectCast(sender, Control).Name).ToString)
MessageBox.Show(DirectCast(sender, Label).Name)
'Dim test As Label = New Label
'test = CType(sender, Label)
''MessageBox.Show(sender.GetType.ToString)
End Sub
End Class
-
Oct 21st, 2009, 10:51 AM
#12
Re: Event Handler For Dynamic Controls
Well thats because you are using my example code without even thinking about what it does... In my example I am showing the Name property of the label but in your code you are never setting the Name property of the labels you create so of course it will be blank when you read that property. You said you wanted to identify which label was clicked so I assumed you had some way of identifying each label (ie, the Name property). If you just wanted to get the label's text then you could do this:
vb Code:
MessageBox.Show(DirectCast(sender, Label).Text)
-
Oct 21st, 2009, 11:54 AM
#13
Thread Starter
Junior Member
Re: Event Handler For Dynamic Controls
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|