473,785 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?? ControlMouseMov e As ??)

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

AddHandler qq.MouseMove, AddressOf ControlMouseMov e

Anyone know how to do that?

Thanks
Nov 21 '05 #1
7 1769

" Just Me" <ne********@a-znet.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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?? ControlMouseMov e As ??)
So I need to pass the address of the handler or equivalent so that I can

AddHandler qq.MouseMove, AddressOf ControlMouseMov e


Try

ByVal ControlMouseMov e As MouseEventHandl er

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.MouseMo ve, AddressOf MyHandler

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

'// 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.s pam> wrote in message
news:u7******** ******@TK2MSFTN GP11.phx.gbl...

" Just Me" <ne********@a-znet.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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?? ControlMouseMov e As

??)

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

AddHandler qq.MouseMove, AddressOf ControlMouseMov e


Try

ByVal ControlMouseMov e As MouseEventHandl er

No guarantees....

Nov 21 '05 #3

"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote in message
news:ev******** ******@TK2MSFTN GP12.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 ControlMouseMov e

Next

AddHandler zzz.MouseMove, AddressOf ControlMouseMov e

Next

AddHandler zz.MouseMove, AddressOf ControlMouseMov e

Next

End Sub

Private Sub ControlMouseMov e(ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs)

sender.Cursor.c urrent = 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******** ******@TK2MSFTN GP12.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.MouseMo ve, AddressOf MyHandler

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

'// 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?? ControlMouseMov e As
??)

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

can
AddHandler qq.MouseMove, AddressOf ControlMouseMov e


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

"Jeff Johnson [MVP: VB]" <i.***@enough.s pam> wrote in message
news:ut******** ******@TK2MSFTN GP10.phx.gbl...

"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote in message news:ev******** ******@TK2MSFTN GP12.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?? ControlMouseMov e As ??)

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

AddHandler qq.MouseMove, AddressOf ControlMouseMov e


\\\
AddMouseDownHan dler(Me.Button1 , AddressOf Button1_MouseDo wn)
..
..
..
Public Sub AddMouseDownHan dler(ByVal EventSource As Control, ByVal Handler As MouseEventHandl er)
AddHandler EventSource.Mou seDown, Handler
End Sub

Private Sub Button1_MouseDo wn(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******** ******@TK2MSFTN GP10.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?? ControlMouseMov e As ??)
So I need to pass the address of the handler or equivalent so that I can

AddHandler qq.MouseMove, AddressOf ControlMouseMov e
\\\
AddMouseDownHan dler(Me.Button1 , AddressOf Button1_MouseDo wn)
.
.
.
Public Sub AddMouseDownHan dler(ByVal EventSource As Control, ByVal Handler

As MouseEventHandl er) AddHandler EventSource.Mou seDown, Handler
End Sub

Private Sub Button1_MouseDo wn(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
18581
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 } Which is called statically by: <button onclick="doClick(event,this);">Click me</button>
2
7678
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 like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this behavior? <!-- main page --> <html> <head> <script type="text/javascript">
3
4885
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 the best method? Do you have a sample of how to do this?
2
2112
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 controls, and populate the dropdowns etc, I use addhandler to define delegates to capture events raised by the controls. I store these controls in a hashtable.
2
3049
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.. only the labels (caption) and the invisible literal controls are binded.. Now based on the invisible literal control values I get the information from DB to bind it with each dropdown list (I am doing this in the item databound event) Also based on...
4
3137
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 controls visible in the onclick event handler, but I do not know how to bring the focus to the bottom section of the page. So basically, when the user clicks the button, everything works correctly in the onclick (all controls are made visible),...
1
2034
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 product types etc. So for instance Im adding a fruit company while adding a fruit company I allow the user to add types of fruit they carry and display it dynamically using an <asp:table> with image
6
412
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 equivalent so that I can AddHandler qq.MouseMove, AddressOf ControlMouseMove Anyone know how to do that?
5
12596
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', makeEventFunc1(strand)); inp.attachEvent('onchange', makeEventFunc2(strand)); individually work in IE, but when used together, only the bottom one remains active. I have also tried
1
2467
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 guy's Javascript "combo box" - http://sandy.mcarthur.org/javascript/select/select.html. It allows my users (when I get some!) to select from a list of preexisting options and also to add a new one by clicking on "add new". Essentially it's a select...
0
9485
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.