473,396 Members | 1,827 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,396 software developers and data experts.

Adding an event handler in a sub

I need to create a library sub that adds event handlers to a control.
Public Shared Sub AddEvent(ByVal qq As Control, By?? ControlMouseMove As ??)

So I need to pass the address of the handler or equivalent so that I can

AddHandler qq.MouseMove, AddressOf ControlMouseMove

Anyone know how to do that?

Thanks
Nov 21 '05 #1
7 1746

" Just Me" <ne********@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I need to create a library sub that adds event handlers to a control.
Public Shared Sub AddEvent(ByVal qq As Control, By?? ControlMouseMove As ??)
So I need to pass the address of the handler or equivalent so that I can

AddHandler qq.MouseMove, AddressOf ControlMouseMove


Try

ByVal ControlMouseMove As MouseEventHandler

No guarantees....
Nov 21 '05 #2
Its a bit more complicated than that really.

Your library code needs to know the event type it is going to handle, and
you need to tell it the address of your delegate function which will handle
it. In order to do this, the function being called will need to know the
reference to the instantiated object ( WithEvents ) which is must handle. So
you must pass it the control reference for which you want the event handled
for.
Public MyHandlerAdd( ByVal myControl as System.Windows.Forms.Control )

AddHandler Control.MouseMove, AddressOf MyHandler

End Sub
Public Sub MyHandler( ByVal Sender as Object, e as System.MouseEventArgs )

'// Do something here

End Sub

This all seems find and dandy, until you realise that you need reference in
the handling sub to the sender ( Which is actually an instance of this class
not the control ).


--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...

" Just Me" <ne********@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I need to create a library sub that adds event handlers to a control.
Public Shared Sub AddEvent(ByVal qq As Control, By?? ControlMouseMove As

??)

So I need to pass the address of the handler or equivalent so that I can

AddHandler qq.MouseMove, AddressOf ControlMouseMove


Try

ByVal ControlMouseMove As MouseEventHandler

No guarantees....

Nov 21 '05 #3

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:ev**************@TK2MSFTNGP12.phx.gbl...
Its a bit more complicated than that really.


Yeah, it seemed too easy. And when I took a close look at the parameters to
AddHandler, I was pretty sure it wouldn't fly.
Nov 21 '05 #4
I was not clear. I wanted to have the handler in the class with the callee,
not with the library, so I needed to pass that address to the library sub.

I try to make my post succinct - sometimes it doesn't work.

This is the code that works inside the class:
////////////////////////////
On Error Resume Next

For Each zz As Control In Controls

For Each zzz As Control In zz.Controls

For Each zzzz As Control In zz.Controls

AddHandler zzzz.MouseMove, AddressOf ControlMouseMove

Next

AddHandler zzz.MouseMove, AddressOf ControlMouseMove

Next

AddHandler zz.MouseMove, AddressOf ControlMouseMove

Next

End Sub

Private Sub ControlMouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)

sender.Cursor.current = CurrentCursor

End Sub

///////////////////////

I'd like to move the top part (not the handeler) to my library.
I suppose it would be great if the event (MouseMove) was also a variable but
don't have the slightest idea how to do that.

Thanks
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:ev**************@TK2MSFTNGP12.phx.gbl...
Its a bit more complicated than that really.

Your library code needs to know the event type it is going to handle, and
you need to tell it the address of your delegate function which will handle it. In order to do this, the function being called will need to know the
reference to the instantiated object ( WithEvents ) which is must handle. So you must pass it the control reference for which you want the event handled for.
Public MyHandlerAdd( ByVal myControl as System.Windows.Forms.Control )

AddHandler Control.MouseMove, AddressOf MyHandler

End Sub
Public Sub MyHandler( ByVal Sender as Object, e as System.MouseEventArgs )

'// Do something here

End Sub

This all seems find and dandy, until you realise that you need reference in the handling sub to the sender ( Which is actually an instance of this class not the control ).

OHM ( Terry Burns )


I need to create a library sub that adds event handlers to a control.
Public Shared Sub AddEvent(ByVal qq As Control, By?? ControlMouseMove As
??)

So I need to pass the address of the handler or equivalent so that I

can
AddHandler qq.MouseMove, AddressOf ControlMouseMove


In the callee class which also contains ControlMouseMove
Nov 21 '05 #5
Thanks anyway

"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:ev**************@TK2MSFTNGP12.phx.gbl...
Its a bit more complicated than that really.
Yeah, it seemed too easy. And when I took a close look at the parameters

to AddHandler, I was pretty sure it wouldn't fly.

Nov 21 '05 #6
* " Just Me" <ne********@a-znet.com> scripsit:
I need to create a library sub that adds event handlers to a control.
Public Shared Sub AddEvent(ByVal qq As Control, By?? ControlMouseMove As ??)

So I need to pass the address of the handler or equivalent so that I can

AddHandler qq.MouseMove, AddressOf ControlMouseMove


\\\
AddMouseDownHandler(Me.Button1, AddressOf Button1_MouseDown)
..
..
..
Public Sub AddMouseDownHandler(ByVal EventSource As Control, ByVal Handler As MouseEventHandler)
AddHandler EventSource.MouseDown, Handler
End Sub

Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
MsgBox("Hello World!")
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #7
Works like a charm

Thanks

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ew**************@TK2MSFTNGP10.phx.gbl...
* " Just Me" <ne********@a-znet.com> scripsit:
I need to create a library sub that adds event handlers to a control.
Public Shared Sub AddEvent(ByVal qq As Control, By?? ControlMouseMove As ??)
So I need to pass the address of the handler or equivalent so that I can

AddHandler qq.MouseMove, AddressOf ControlMouseMove
\\\
AddMouseDownHandler(Me.Button1, AddressOf Button1_MouseDown)
.
.
.
Public Sub AddMouseDownHandler(ByVal EventSource As Control, ByVal Handler

As MouseEventHandler) AddHandler EventSource.MouseDown, Handler
End Sub

Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) MsgBox("Hello World!")
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
2
by: Tim Marsden | last post by:
Hi, This is what I am doing, please comment if this is the correct way. I need to add controls to a form dynamically. Within the Page_Load event (is not Postback) I run a routine to create the...
2
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.....
4
by: PK9 | last post by:
I have a button at the top of my page that I have an onClick event handler for which makes some new controls (at the bottom of my page) visible. When the user clicks the button I make the new...
1
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
6
by: Just Me | last post by:
I need to create a library sub that adds event handlers to a control. Public Shared Sub AddEvent(ByVal qq As Control, By?? ControlMouseMove As ??) So I need to pass the address of the handler or...
5
by: J | last post by:
I am having problems dynamically adding more than one event handler to an input. I have tried the Javascript included at the bottom. The lines inp.attachEvent('onkeyup',...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.