Referring to a run-time generated object?
Hi all, I have a button on my form which can generate an unlimited number of panels.
Code:
Dim pnl As New Panel With {.BorderStyle = BorderStyle.FixedSingle, .BackColor = Color.Gainsboro, .Tag = p}
If ProcessPanels.Count = 0 Then
pnl.Bounds = New Rectangle(12, 45, 900, 500)
Else
pnl.Bounds = New Rectangle(ProcessPanels(ProcessPanels.Count - 1).Right + 12, 45, 900, 500)
End If
Code:
Me.Controls.Add(pnl)
How do I refer to this in a private sub (baring in mind that I have an unlimited amount that can be created and need to be able to refer to each independently, as well as, in some cases, refer to all at the same time.
Specifically, I want to basically do this:
Code:
Private Sub pnl_MouseEnter(sender As Object, e As EventArgs) Handles pnl.MouseEnter
Obviously the above does not work as the object does not exist when I run the application (yet).
Thanks very much for the help!
Re: Referring to a run-time generated object?
Get rid of the Handles clause on that event handler method but everything else will be the same. When you create the Panel, register the event handler like this:
vb.net Code:
AddHandler pnl.MouseEnter, AddressOf pnl_MouseEnter
Inside the event handler, you can get access to the Panel that raised the event via the 'sender' parameter:
vb.net Code:
Dim pnl = DirectCast(sender, Panel)
Re: Referring to a run-time generated object?
Had the same solution as jmcilhinney ,
but got out posted when i was typing the solution...
Re: Referring to a run-time generated object?
If you create the event handler, minus the Handles bit you can always connect it up at runtime. e.g.
Code:
Private Sub pnl_MouseEnter(sender As Object, e As EventArgs)
dim pnl as Panel = DirectCast(sender, Panel)
'whatever
End Sub
Code:
Dim pnl As New Panel With {.BorderStyle = BorderStyle.FixedSingle, .BackColor = Color.Gainsboro, .Tag = p}
If ProcessPanels.Count = 0 Then
pnl.Bounds = New Rectangle(12, 45, 900, 500)
Else
pnl.Bounds = New Rectangle(ProcessPanels(ProcessPanels.Count - 1).Right + 12, 45, 900, 500)
End If
AddHandler pnl.MouseEnter, AddressOf pnl_MouseEnter
When the event handler fires, the sender argument will refer to the panel that triggered the event, you would just need to cast it to a panel object to use it.
Edit: Two people type quicker than me it would seem...