Withevents for dynamicly created controls
-
Hi there! I need some help with events for dynamicly created objects. The code below illustrates the problem. My situation will be a little different since the objects I will be dynamicly creating will be custom, but the problem is the same. The event will only fire on the last created object. How can I have an event fired within the form Class each time an individual instance of the label is clicked??
Public Class Form1
Friend WithEvents lbl As Label
Private i As IntegerPrivate Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load i = 1 End Sub Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp lbl = New Label lbl.Name = "lbl" & i lbl.Text = "Label " & i lbl.Location = e.Location lbl.AutoSize = True Me.Controls.Add(lbl) i += 1 End Sub Private Sub lbl\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbl.Click MsgBox(lbl.Text) End Sub
End Class
-
Hi there! I need some help with events for dynamicly created objects. The code below illustrates the problem. My situation will be a little different since the objects I will be dynamicly creating will be custom, but the problem is the same. The event will only fire on the last created object. How can I have an event fired within the form Class each time an individual instance of the label is clicked??
Public Class Form1
Friend WithEvents lbl As Label
Private i As IntegerPrivate Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load i = 1 End Sub Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp lbl = New Label lbl.Name = "lbl" & i lbl.Text = "Label " & i lbl.Location = e.Location lbl.AutoSize = True Me.Controls.Add(lbl) i += 1 End Sub Private Sub lbl\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbl.Click MsgBox(lbl.Text) End Sub
End Class
-
Hi there! I need some help with events for dynamicly created objects. The code below illustrates the problem. My situation will be a little different since the objects I will be dynamicly creating will be custom, but the problem is the same. The event will only fire on the last created object. How can I have an event fired within the form Class each time an individual instance of the label is clicked??
Public Class Form1
Friend WithEvents lbl As Label
Private i As IntegerPrivate Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load i = 1 End Sub Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp lbl = New Label lbl.Name = "lbl" & i lbl.Text = "Label " & i lbl.Location = e.Location lbl.AutoSize = True Me.Controls.Add(lbl) i += 1 End Sub Private Sub lbl\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbl.Click MsgBox(lbl.Text) End Sub
End Class
This code won't work. You only have one label wired up with it. Basically, you can't use WithEvents on a class-level variable and expect this to work. Drop the "Friend WithEvents..." line completely. Your MouseUp code has to create the label and wire up the events itself. This way, every label you create will maintain event connections.
Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp Dim lbl As New Label ' lbl.Name = "lbl" & i ' Drop this line. It's not necessary lbl.Text = "Label " & i lbl.Location = e.Location lbl.AutoSize = True AddHandler lbl.Click, AddressOf lbl\_Click Me.Controls.Add(lbl) i += 1 End Sub
and drop the "Handles lbl.Click" from the end of the Sub you defined.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
This code won't work. You only have one label wired up with it. Basically, you can't use WithEvents on a class-level variable and expect this to work. Drop the "Friend WithEvents..." line completely. Your MouseUp code has to create the label and wire up the events itself. This way, every label you create will maintain event connections.
Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp Dim lbl As New Label ' lbl.Name = "lbl" & i ' Drop this line. It's not necessary lbl.Text = "Label " & i lbl.Location = e.Location lbl.AutoSize = True AddHandler lbl.Click, AddressOf lbl\_Click Me.Controls.Add(lbl) i += 1 End Sub
and drop the "Handles lbl.Click" from the end of the Sub you defined.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Works great! thanks for your help.