Events for controls declared in code

  • Thread starter Thread starter Robert
  • Start date Start date
R

Robert

I have created an array of panels (dim panels(100) as new
panel). I've succesfully loaded them on the form.

What code should I write in order to have acces to the
mouseover event for each of the panels (because I can't
double-click on them and select this event). I want to be
able to control each of the panels' reaction to the
mouseover event by identifying them through an INDEX.
Just like in VB 6.0. :P
 
Hi Robert,

This should give you the idea. It is typed here from two different samples I
made so watch typos

I hope this helps?

Cor
\\\
Private Sub Form5_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
End Sub
Private Sub doSet(ByVal parentCtr As Control)
For Each ctr as Control In parentCtr.Controls
if typeof ctr Is Panel then
AddHandler ctr.MouseLeave, AddressOf meMouseLeave
AddHandler ctr.GotFocus, AddressOf meMouseEnter
end if
doSet(ctr)
Next
End Sub
Private Sub meMouseLeave(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
Me.Panel1.BackColor = Color.Blue
End Sub
Private Sub meMouseEnter(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles
Me.Panel1.BackColor = Color.Red
End Sub
////
 
I have created an array of panels (dim panels(100) as new
panel). I've succesfully loaded them on the form.

What code should I write in order to have acces to the
mouseover event for each of the panels (because I can't
double-click on them and select this event). I want to be
able to control each of the panels' reaction to the
mouseover event by identifying them through an INDEX.
Just like in VB 6.0. :P

You'd use the AddHandler statement to wire up each panel you add to a SINGLE
procedure that you'll supply. Then in the code of that procedure you'll
check the sender argument against your array, like this:

For x = 0 To 100
If sender Is panels(x) Then
' Now you know which panel you have based on x
End If
Next

That's the long way. It would more efficient to set the Tag of each panel
you add and then use that as a key into a HashTable instead of looping
through an array each time. Or, now that I think about it more, just set the
Tag to the index of the panel as you add it and then use the tag to index
into the array. Yeah, that's the ticket....
 
And because I typed it over of course a big error in it the last part has to
be something as

\\\
Private Sub meMouseLeave(ByVal sender _
As Object, ByVal e As System.EventArgs)
directcast(sender, panel).BackColor = Color.Blue
dim myPanelName as string = directcast(sender, panel).text
End Sub
Private Sub meMouseEnter(ByVal sender _
As Object, ByVal e As System.EventArgs)
directcast(sender, panel).BackColor = Color.Blue
dim myPanelName as string = directcast(sender, panel).text
End Sub
///
Cor
 
* "Robert said:
I have created an array of panels (dim panels(100) as new
panel). I've succesfully loaded them on the form.

What code should I write in order to have acces to the
mouseover event for each of the panels (because I can't
double-click on them and select this event). I want to be
able to control each of the panels' reaction to the
mouseover event by identifying them through an INDEX.

The easiest way to do that is to add an event handler to the controls
using 'AddHandler' (only one handler that is shared by all the
controls). The parameter 'sender' of the event handler will contain a
reference to the control that raised the event. You can cast it to
'Control' or the type of your controls ('Panel') to access its members.
 
The easiest way to do that is to add an event handler to the controls
using 'AddHandler' (only one handler that is shared by all the
controls). The parameter 'sender' of the event handler will contain a
reference to the control that raised the event. You can cast it to
'Control' or the type of your controls ('Panel') to access its members.

Herfried brings up a good point: if you're only interested in identifying
the control that threw the event because you want to do something with it
(like change its BackColor), .NET has already done this for you with the
sender argument. I was going on a larger assumption that you needed to do
certain things with certain panels and other things with other panels.
 
I've tried what you wrote here but it doesn't seem to
work, VB doesn't compile my application.

i've wrote something like this:

dim x(3) as label

in the form load i've written:

for i=1 to 3
x(i)=new label
controls.add(x(i))
addhandler x(i).mousemove,proced(x(i))

and:

function proced(byval sender as label)
For i = 0 To 3
If sender Is mex(i) Then
MsgBox("cuccs")
End If
Next
end function

This code doesn't work, the application is not compiled
by VB.
 
Missed something. The handler MUST have the correct signature:
function proced(byval sender as label)

Function proced(ByVal sender As Object, ByVal e As MouseEventArgs)
 
Back
Top