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

Are event arguments needed for button control

When you drop a button on a form, a Click event is created by the designer.
Within the Click event certain event parameters are defined:
Private Sub btnInsert_ButtonPressed(ByVal sender As System.Object, ByVal e
As System.EventArgs)

What harm or limitations would ensue if (ByVal sender As System.Object,
ByVal e As System.EventArgs) were changed to ().

Thanks,
Dean S

Jul 18 '08 #1
6 1213
On Jul 17, 8:49*pm, "Dean Slindee" <slin...@charter.netwrote:
When you drop a button on a form, a Click event is created by the designer.
Within the Click event certain event parameters are defined:
Private Sub btnInsert_ButtonPressed(ByVal sender As System.Object, ByVal e
As System.EventArgs)

What harm or limitations would ensue if (ByVal sender As System.Object,
ByVal e As System.EventArgs) were changed to ().

Thanks,
Dean S
Try it and find out. There is no better way of learning.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jul 18 '08 #2
Dean Slindee wrote:
When you drop a button on a form, a Click event is created by the
designer. Within the Click event certain event parameters are defined:
Private Sub btnInsert_ButtonPressed(ByVal sender As System.Object, ByVal
e As System.EventArgs)

What harm or limitations would ensue if (ByVal sender As System.Object,
ByVal e As System.EventArgs) were changed to ().
First guess - it wouldn't compile.
(actually VB'2008 might do some "clever" stuff; haven't got there yet)

VB wires up events using "delegates" and delegates are just methods with
a particular signature. If you don't supply the right arguments
(signature) for a method, VB can't wire up the event to that method (the
error is something like "method X cannot handle event Y").

I'm guessing that, like me, you're a VB "proper" Developer and I can see
what you're thinking - /why/ do you need these extraneous arguments?

Indeed, in the case you cite, there's not a lot of point but there's two
things to bear in mind:

(1) Other events /do/ use meaningful Event Argument classes, not just
the base class, EventArgs - take a look at, say, the Layout event; that
supplies your event handler with lots of useful stuff.

(2) Under VB "Proper" if you wrote six buttons, you'd get six *_Click
routines (ignoring Control Arrays). In VB.Net, you can have a /single/
method handling the click event of /many/ buttons, as in:

Private Sub AnyButton_Click( ... ) _
Handles btnInsert.Click _
, btn5_Click _
, btn10_Click _
, btn15_Click _
, ... and so on ...

So how does this routine know /which/ button you actually clicked on?
That's what "sender" argument is for - you get an object reference to
the control (here, a button) that raised the event, as in

Private Sub AnyButton_Click( ... ) _
Handles btnInsert.Click, ...

If ( TypeOf sender Is Button ) Then
MessageBox.Show( "You clicked " _
& DirectCast( sender, Button ).Name _
)
End If

End Sub

HTH,
Phill W.
Jul 18 '08 #3
The worst is that afterwards, double clicking on the control does not go
to the code any more.

Mike

On Fri, 18 Jul 2008 13:53:08 +0100, in
microsoft.public.dotnet.languages.vb "Phill W."
<p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote:
>Dean Slindee wrote:
>When you drop a button on a form, a Click event is created by the
designer. Within the Click event certain event parameters are defined:
Private Sub btnInsert_ButtonPressed(ByVal sender As System.Object, ByVal
e As System.EventArgs)

What harm or limitations would ensue if (ByVal sender As System.Object,
ByVal e As System.EventArgs) were changed to ().

First guess - it wouldn't compile.
(actually VB'2008 might do some "clever" stuff; haven't got there yet)

VB wires up events using "delegates" and delegates are just methods with
a particular signature. If you don't supply the right arguments
(signature) for a method, VB can't wire up the event to that method (the
error is something like "method X cannot handle event Y").

I'm guessing that, like me, you're a VB "proper" Developer and I can see
what you're thinking - /why/ do you need these extraneous arguments?

Indeed, in the case you cite, there's not a lot of point but there's two
things to bear in mind:

(1) Other events /do/ use meaningful Event Argument classes, not just
the base class, EventArgs - take a look at, say, the Layout event; that
supplies your event handler with lots of useful stuff.

(2) Under VB "Proper" if you wrote six buttons, you'd get six *_Click
routines (ignoring Control Arrays). In VB.Net, you can have a /single/
method handling the click event of /many/ buttons, as in:

Private Sub AnyButton_Click( ... ) _
Handles btnInsert.Click _
, btn5_Click _
, btn10_Click _
, btn15_Click _
, ... and so on ...

So how does this routine know /which/ button you actually clicked on?
That's what "sender" argument is for - you get an object reference to
the control (here, a button) that raised the event, as in

Private Sub AnyButton_Click( ... ) _
Handles btnInsert.Click, ...

If ( TypeOf sender Is Button ) Then
MessageBox.Show( "You clicked " _
& DirectCast( sender, Button ).Name _
)
End If

End Sub

HTH,
Phill W.
Jul 19 '08 #4
On Jul 18, 3:49 am, "Dean Slindee" <slin...@charter.netwrote:
When you drop a button on a form, a Click event is created by the designer.
Within the Click event certain event parameters are defined:
Private Sub btnInsert_ButtonPressed(ByVal sender As System.Object, ByVal e
As System.EventArgs)

What harm or limitations would ensue if (ByVal sender As System.Object,
ByVal e As System.EventArgs) were changed to ().

Thanks,
Dean S
Hi,
Simply, it wouldn't compile because it's expected that your button's
method(default button1_click) requires the same signature of "click"
event's , that is, "ByVal sender As System.Object, ByVal e As
System.EventArgs".

Thanks,

Onur Güzel

Jul 19 '08 #5
In opposition to what someone posted, it WILL run just find. But you
can't double click you way to the code any more.

As with all these kind of questions, trying it is the best way to find
out.

Mike
On Sat, 19 Jul 2008 02:19:09 -0700, in
microsoft.public.dotnet.languages.vb Ju********@home.net wrote:
>The worst is that afterwards, double clicking on the control does not go
to the code any more.

Mike

On Fri, 18 Jul 2008 13:53:08 +0100, in
microsoft.public.dotnet.languages.vb "Phill W."
<p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote:
>>Dean Slindee wrote:
>>When you drop a button on a form, a Click event is created by the
designer. Within the Click event certain event parameters are defined:
Private Sub btnInsert_ButtonPressed(ByVal sender As System.Object, ByVal
e As System.EventArgs)

What harm or limitations would ensue if (ByVal sender As System.Object,
ByVal e As System.EventArgs) were changed to ().

First guess - it wouldn't compile.
(actually VB'2008 might do some "clever" stuff; haven't got there yet)

VB wires up events using "delegates" and delegates are just methods with
a particular signature. If you don't supply the right arguments
(signature) for a method, VB can't wire up the event to that method (the
error is something like "method X cannot handle event Y").

I'm guessing that, like me, you're a VB "proper" Developer and I can see
what you're thinking - /why/ do you need these extraneous arguments?

Indeed, in the case you cite, there's not a lot of point but there's two
things to bear in mind:

(1) Other events /do/ use meaningful Event Argument classes, not just
the base class, EventArgs - take a look at, say, the Layout event; that
supplies your event handler with lots of useful stuff.

(2) Under VB "Proper" if you wrote six buttons, you'd get six *_Click
routines (ignoring Control Arrays). In VB.Net, you can have a /single/
method handling the click event of /many/ buttons, as in:

Private Sub AnyButton_Click( ... ) _
Handles btnInsert.Click _
, btn5_Click _
, btn10_Click _
, btn15_Click _
, ... and so on ...

So how does this routine know /which/ button you actually clicked on?
That's what "sender" argument is for - you get an object reference to
the control (here, a button) that raised the event, as in

Private Sub AnyButton_Click( ... ) _
Handles btnInsert.Click, ...

If ( TypeOf sender Is Button ) Then
MessageBox.Show( "You clicked " _
& DirectCast( sender, Button ).Name _
)
End If

End Sub

HTH,
Phill W.
Jul 19 '08 #6
The reason for asking was that the event did compile in VS2008 after the
arguments were removed.
Thanks so much for your excellent answer.

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:g5**********@south.jnrs.ja.net...
Dean Slindee wrote:
>When you drop a button on a form, a Click event is created by the
designer. Within the Click event certain event parameters are defined:
Private Sub btnInsert_ButtonPressed(ByVal sender As System.Object, ByVal
e As System.EventArgs)

What harm or limitations would ensue if (ByVal sender As System.Object,
ByVal e As System.EventArgs) were changed to ().

First guess - it wouldn't compile.
(actually VB'2008 might do some "clever" stuff; haven't got there yet)

VB wires up events using "delegates" and delegates are just methods with a
particular signature. If you don't supply the right arguments (signature)
for a method, VB can't wire up the event to that method (the error is
something like "method X cannot handle event Y").

I'm guessing that, like me, you're a VB "proper" Developer and I can see
what you're thinking - /why/ do you need these extraneous arguments?

Indeed, in the case you cite, there's not a lot of point but there's two
things to bear in mind:

(1) Other events /do/ use meaningful Event Argument classes, not just the
base class, EventArgs - take a look at, say, the Layout event; that
supplies your event handler with lots of useful stuff.

(2) Under VB "Proper" if you wrote six buttons, you'd get six *_Click
routines (ignoring Control Arrays). In VB.Net, you can have a /single/
method handling the click event of /many/ buttons, as in:

Private Sub AnyButton_Click( ... ) _
Handles btnInsert.Click _
, btn5_Click _
, btn10_Click _
, btn15_Click _
, ... and so on ...

So how does this routine know /which/ button you actually clicked on?
That's what "sender" argument is for - you get an object reference to the
control (here, a button) that raised the event, as in

Private Sub AnyButton_Click( ... ) _
Handles btnInsert.Click, ...

If ( TypeOf sender Is Button ) Then
MessageBox.Show( "You clicked " _
& DirectCast( sender, Button ).Name _
)
End If

End Sub

HTH,
Phill W.
Jul 20 '08 #7

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

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
3
by: N. Demos | last post by:
How do you dynamically assign a function to an element's event with specific parameters? I know that the code is different for MSIE and Mozilla, and need to know how to do this for both. I...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
3
by: jeff29_b | last post by:
I am having a strange problem on a web form. I have an image button with an OnClick event handler. When I click the image the event isn't being called in the code behind when browsing in firefox....
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
5
by: james | last post by:
Hello, I am having a little trouble creating an event handler for a context menu toolstripmenuitem. I've seen various tutorials and so on, but I keep getting a bit stuck! So far I have a second...
3
by: franc sutherland | last post by:
Hello, I have a report which I filter using the me.filter command in the OnOpen event. Me.Filter = "OrderID=" & Forms!variable_form_name! Me.FilterOn = True I want to be able to open that...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.