By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

Events for controls declared in code

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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
////

Nov 20 '05 #2

P: n/a

"Robert" <an*******@discussions.microsoft.com> wrote in message
news:2e*****************************@phx.gbl...
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....
Nov 20 '05 #3

P: n/a
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
Nov 20 '05 #4

P: n/a
* "Robert" <an*******@discussions.microsoft.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O6**************@tk2msftngp13.phx.gbl...
* "Robert" <an*******@discussions.microsoft.com> scripsit:
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.


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.
Nov 20 '05 #6

P: n/a
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.

-----Original Message-----

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in messagenews:O6**************@tk2msftngp13.phx.gbl...
* "Robert" <an*******@discussions.microsoft.com> scripsit:
> 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.
Herfried brings up a good point: if you're only interested in identifyingthe 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 thesender argument. I was going on a larger assumption that you needed to docertain things with certain panels and other things with other panels.

.

Nov 20 '05 #7

P: n/a

"Robert" <an*******@discussions.microsoft.com> wrote in message
news:2e*****************************@phx.gbl...
addhandler x(i).mousemove,proced(x(i))


Just

AddHandler x(i).MouseMove, proced
Nov 20 '05 #8

P: n/a

"Robert" <an*******@discussions.microsoft.com> wrote in message
news:2e*****************************@phx.gbl...

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)
Nov 20 '05 #9

P: n/a
Jeff,

* "Jeff Johnson [MVP: VB]" <i.***@enough.spam> scripsit:
"Robert" <an*******@discussions.microsoft.com> wrote in message
news:2e*****************************@phx.gbl...
addhandler x(i).mousemove,proced(x(i))


Just

AddHandler x(i).MouseMove, proced


I think there is an 'AddressOf' missing in front of the 'proced'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #10

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
* "Jeff Johnson [MVP: VB]" <i.***@enough.spam> scripsit:


Hahaha. I just noticed the Latin in the quote line....

addhandler x(i).mousemove,proced(x(i))


Just

AddHandler x(i).MouseMove, proced


I think there is an 'AddressOf' missing in front of the 'proced'.


AARRGGHH!! I'll get used to this stuff sooner or later.
Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.