473,396 Members | 2,033 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.

Is there a good way to write for events under VB?

Hello, experts:

I have a beginner's question. I would like to know if there is a sensible way
of writing event handlers under VB. For a Numeric Update control called
"nudSpin", this is apparently wrong:

Private Sub nudSpin_MouseUp() Handles nudSpin.MouseUp
' my code
End Sub

I get an error which says "Method 'nudSpin_MouseUp' cannot handle Event
'MouseUp' because they do not have the same signature."

OK, so what is this signature stuff about? If I create the method header the
old fashioned (VB6) way (by clicking on the control), I get:

Private Sub nudSpin_ValueChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles nudSpin.ValueChanged
' place code here
End Sub

which is not what I want. I don't want a response to a changed value, I want a
response to someone releasing a mouse button while using the control. If I
changed the "Handles" modifier to "Handles nudSpin.MouseUp, I get the
signature error again.

I don't get this signature stuff. I am not using prorpietary controls, and
after reading warnings on this newsgroup, I have avoided ActiveX controls
(even though VB tells me they are available). The controls for this program
appear to be native to Microsoft VB.NET. Why can't I handle the events I want
to handle?

Thanks for any light you can shed on this

Paul King
Jun 23 '07 #1
5 1890
By looking around the documentation, it looks as though the initial problem
was overcome (by cutting and pasting), but not the larger problem of writing
methods generally.

It seems that fussing about "signatures" is Microsoft's way of saying the
parameters don't match the prototype. The word "signature" is too confusing,
since the same word is used in many other contexts (even in this newsgroup).
That is, nothing I was using needed any digital signing.

However, the question remains, if I need to write a specialised method, do I
need to memorise all possible syntaxes to all events for all controls, do I
cut and paste the syntax from the Help dialog, or is there something more
efficient? Obviously, clicking on the control doesn't give me the sensible
methods it used to give me.

Paul King

On Sat, 23 Jun 2007 15:18:13 -0400, Paul King <xo*****@hotmail.comwrote:
>Hello, experts:

I have a beginner's question. I would like to know if there is a sensible way
of writing event handlers under VB. For a Numeric Update control called
"nudSpin", this is apparently wrong:

Private Sub nudSpin_MouseUp() Handles nudSpin.MouseUp
' my code
End Sub

I get an error which says "Method 'nudSpin_MouseUp' cannot handle Event
'MouseUp' because they do not have the same signature."

OK, so what is this signature stuff about? If I create the method header the
old fashioned (VB6) way (by clicking on the control), I get:

Private Sub nudSpin_ValueChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles nudSpin.ValueChanged
' place code here
End Sub

which is not what I want. I don't want a response to a changed value, I want a
response to someone releasing a mouse button while using the control. If I
changed the "Handles" modifier to "Handles nudSpin.MouseUp, I get the
signature error again.

I don't get this signature stuff. I am not using prorpietary controls, and
after reading warnings on this newsgroup, I have avoided ActiveX controls
(even though VB tells me they are available). The controls for this program
appear to be native to Microsoft VB.NET. Why can't I handle the events I want
to handle?

Thanks for any light you can shed on this

Paul King
Jun 23 '07 #2
Hello, experts:
>
I have a beginner's question. I would like to know if there is a
sensible way of writing event handlers under VB. For a Numeric Update
control called "nudSpin", this is apparently wrong:

Private Sub nudSpin_MouseUp() Handles nudSpin.MouseUp
' my code
End Sub
I get an error which says "Method 'nudSpin_MouseUp' cannot handle
Event 'MouseUp' because they do not have the same signature."

OK, so what is this signature stuff about?
Ok all events that are possible to code against have already been defined.
You do not get to say what that signature is. you must match by parameter
quantity and type.

If you wish to handle the Mouseup event then there are 2 ways to do it.

1.Define the instance of the control/class that you wish to handle events
for using the WithEvents keyword.
This will already have been done for you if you dragged the control onto
the form in the forst place.
Once done you can select the control from the dropdown in the top left of
the IDE and then select the event from the next dropdown in the top right.
This creates the Stub signature where you can fill out you handling code.

This can be done manually if you prefer. All you need to get right is the
params with their types and the handles keyword together with its Object.event
syntax at tyhe end.

2.Skip the "WithEvents" and "Handles" aspects but still code with the correct
params and their types. Then Attach the coded method to the object's event
using the AddHandler syntax
-------------------------------------------------------------
AddHandler <Object>.<Event>, Addressof <ProcName>
-------------------------------------------------------------

Hopefully this should set you on the right track.

If you need anything else... just ask :)

--
Rory
Jun 23 '07 #3

"Paul King" <xo*****@hotmail.comwrote in message
news:3o********************************@4ax.com...
Hello, experts:

I have a beginner's question. I would like to know if there is a sensible
way
of writing event handlers under VB. For a Numeric Update control called
"nudSpin", this is apparently wrong:

Private Sub nudSpin_MouseUp() Handles nudSpin.MouseUp
' my code
End Sub

I get an error which says "Method 'nudSpin_MouseUp' cannot handle Event
'MouseUp' because they do not have the same signature."

OK, so what is this signature stuff about? If I create the method header
the
old fashioned (VB6) way (by clicking on the control), I get:

Private Sub nudSpin_ValueChanged(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles nudSpin.ValueChanged
' place code here
End Sub

which is not what I want. I don't want a response to a changed value, I
want a
response to someone releasing a mouse button while using the control. If I
changed the "Handles" modifier to "Handles nudSpin.MouseUp, I get the
signature error again.

I don't get this signature stuff. I am not using prorpietary controls, and
after reading warnings on this newsgroup, I have avoided ActiveX controls
(even though VB tells me they are available). The controls for this
program
appear to be native to Microsoft VB.NET. Why can't I handle the events I
want
to handle?

Thanks for any light you can shed on this

Paul King
The signature is the set of parameters sent by the assembly which is raising
the event. The first parameter is the control which is sending the event
and the second parameter depends on the type of event. For example a click
event for a button does not send any other info so the second parameter is
not used. For other events it will send information pertinant to the event.

When you double click a control in the designer you will get the "default"
event created. Just go to the list of controls in the dropdown at the top
of the source code, select the control you want to create an event for and
then from the dropdown beside it select the event you wish to create code
for. It is really not much different than VB6.

Lloyd Sheen

Jun 23 '07 #4
On Sat, 23 Jun 2007 19:36:20 +0000 (UTC), Rory Becker
<Ro********@newsgroup.nospamwrote:
>If you wish to handle the Mouseup event then there are 2 ways to do it.

1.Define the instance of the control/class that you wish to handle events
for using the WithEvents keyword.
This will already have been done for you if you dragged the control onto
the form in the forst place.
Once done you can select the control from the dropdown in the top left of
the IDE and then select the event from the next dropdown in the top right.
This creates the Stub signature where you can fill out you handling code.
Thanks. I forgot about this from VB6.

Paul King
Jun 23 '07 #5
Paul King wrote:
I have a beginner's question. I would like to know if there is a sensible way
of writing event handlers under VB.
There is a standard convention for event handlers that permeates the
entire Framework - whether you regard this as "sensible" is another
matter entirely.
For a Numeric Update control called "nudSpin", this is apparently wrong:
Private Sub nudSpin_MouseUp() Handles nudSpin.MouseUp
The Event model changed /completely/ from VB6, which used what is now
referred to as an "auto-wireup" model. You /used/ to have ...

Sub nudSpin_ValueChanged()

.... and /just/ because this method /happened/ to have the name of the
Control, followed by "_", followed by the name of the Event, the VB
compiler "hooked it up" to the ValueChanged event on the control,
nudSpin. This was the /only/ method that could handle that particular
event.

Now, in Brave New World.Net, we have the "new", "improved" event model,
using complicated signatures and the Handles keyword.

Sub nudSpin_ValueChanged( _
ByVal sender as Object _
, ByVal e as EventArgs) _
Handles nudSpin.ValueChanged

In every event handler, "sender" is the Control (or other object) that
raised the event, whatever that happened to be, and "e" is an
EventArgs-derived class that contains useful information about the event
(or EventArgs.Empty, if there's nothing useful to tell you).

However, in this mew way of thinking, you could just as well have ...

Sub fred66( _
ByVal sender as Object _
, ByVal e as EventArgs _
) Handles nudSpin1.ValueChanged _
, nudSpin2.ValueChanged _
, nudSpin3.ValueChanged

.... one routine handling the ValueChanged event from three /different/
spin controls.
OK, so what is this signature stuff about?
Where we just used to have method Names, we now have Signatures - the
combination of the method Name and all its arguments. This is how
overloading works. Compare the signatures for the two Events:

Sub nudSpin_MouseUp( sender as Object, e as MouseEventArgs )
Sub nudSpin_ValueChanged( sender as Object, e as EventArgs )

Different signatures, different methods.
You can't "cross-wire" one event into a handler for another - the code
in the event handler will probably rely on certain properties being
present in the event arguments, "e", and if you send it the wrong Type
of event argument, that code will fail - the compiler is trying to
protect you from doing this when it complains about methods not being
able to handle one another's events.
I don't get this signature stuff.
It /does/ take a bit of getting used to...
Why can't I handle the events I want to handle?
You can; it's just a mite more fiddly to do that it used to be.

HTH,
Phill W.
Jun 25 '07 #6

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

Similar topics

1
by: David Thompson | last post by:
Looking for a book to help me develop a philosophy for building databases (particularly on MySQL). And then taking them from concept to construction. Something like.... Start by asking which...
19
by: Raposa Velha | last post by:
Hello to all! Does any of you want to comment the approach I implement for instantiating a form? A description and an example follow. Cheers, RV jmclopesAThotmail.com replace the AT with the...
15
by: John Salerno | last post by:
Ok, I've been reading an intro book to C# and I'm learning a lot of the basics. I can follow along with all the sample code and understand exactly what it's doing, but I know there's no way I'd be...
2
by: Anton Sommer | last post by:
Hello folks, what is the .NET framework snapin in IE good for? I mean in the Security settings of IE you can activate the execution of .NET framework components. How could a .NET developer...
1
by: Wootaek Choi | last post by:
Dear Sir, I create webform page that all html code is generated in code behind and sended to IE using Response.Write() like this, namespace localhost.XML { public class Encoding :...
1
by: jamie | last post by:
Happy holidays all. :) Okay now for the "more serious" stuff... Is there any good books for the .net classes? I am trying to find when piticular events are fired and from where. It is kinda...
24
by: Gaijinco | last post by:
I found one of that problems all of us have solve when they begin programming: given 3 numbers print the greater and the lesser one of the set. I was trying to remember the if-then-else...
7
by: Rob R. Ainscough | last post by:
I'm having difficulty writing anything to the web servers event log from my web app located on that server. Putting this in my web apps web.config did NOT help - immediate causes and error in my...
16
by: AAaron123 | last post by:
I have a timer. At each tick, say 0.1 second, I write a file. If it takes more than 0.1 second to write the file the app will not work correctly. How can I tell in the tick event if the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.