473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Events for controls declared in code

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
10 1119
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

"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
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
* "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

"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
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

"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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
5
by: karthick raja | last post by:
Am experiencing a problem intercepting the events from controls added dynamically to a Placeholder control at runtime. Is there any way that I can write an event handler on the page which will be...
2
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
18
by: Brett | last post by:
What are reasons to create your own events? Why not just call a class method/function instead? Thanks, Brett
5
by: snesbit | last post by:
If a screen is made up of several user controls and those user controls contain various packaged or standard controls such as a grid, how do you raise both standard and custom events from the user...
8
by: Xero | last post by:
When my program is launched, there is a textbox on the form. When the user enters a character into the textbox (TextChanged), a second, declared textbox is added using this block of code. Dim...
6
by: Steve Hershoff | last post by:
Hi everyone, I've got a strange one here. There are two datagrids on my page, one nested within the other. I'll refer to them as the topmost and secondary datagrids. In the topmost...
4
by: Joergen Bech | last post by:
I sometimes use delegates for broadcasting "StateChanged" events, i.e. if I have multiple forms and/or controls that need updating at the same time as the result of a change in a global/common...
3
by: Anane | last post by:
I get following error when i press a command button. The expression click you entered as the event property setting produced the following error: method or data member not found. It gives me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.