By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,065 Members | 1,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,065 IT Pros & Developers. It's quick & easy.

How do I press a command button in code?

P: n/a
In VB6 all you needed to do was set the name of the command button to True
like:

cmdGo = True

but this does not work in vb.net

Is there a code sample that shows how to do this?
Nov 21 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"Darrell Wesley" <Da***********@discussions.microsoft.com> schrieb:
In VB6 all you needed to do was set the name of the command button to True
like:

cmdGo = True

but this does not work in vb.net


\\\
Me.Command1.PerformClick()
///

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

Nov 21 '05 #2

P: n/a
Hello Darrell,

For calling a button press event pass all the required parameters: here is
an example:

Command1_click(nothing,nothing) 'We are passing nothing becasue we dont want
nothing to be passed.

If you want to pass some parameters you may do here.

I hope this is helpful

Thanks
Mona
[Grapecity]

"Darrell Wesley" <Da***********@discussions.microsoft.com> wrote in message
news:3A**********************************@microsof t.com...
In VB6 all you needed to do was set the name of the command button to True
like:

cmdGo = True

but this does not work in vb.net

Is there a code sample that shows how to do this?

Nov 21 '05 #3

P: n/a
Mona,

"Mona" <mo**@discussions.com> schrieb:
For calling a button press event pass all the required parameters: here is
an example:

Command1_click(nothing,nothing) 'We are passing nothing becasue we dont
want
nothing to be passed.


I recommend to pass the "correct" parameter values in order to prevent
unforseeable problems from occurring if the event handler accesses its
parameter's values in future. To do so, pass a reference to the control
raising the event in the first paramter and 'EventArgs.Empty' in the second
parameter in the case of the 'Click' event.

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

Nov 21 '05 #4

P: n/a
In VB 6.0 you didn't need to invoke the event, nor do you need to in VB
..NET. In both (VB 6.0 and in VB .NET), you simply call the event handler
and pass the appropriate parameters.
"Darrell Wesley" <Da***********@discussions.microsoft.com> wrote in message
news:3A**********************************@microsof t.com...
In VB6 all you needed to do was set the name of the command button to True
like:

cmdGo = True

but this does not work in vb.net

Is there a code sample that shows how to do this?

Nov 21 '05 #5

P: n/a
Yeah. I agree with Herfried using PerformClick()

Crouchie1998
BA (HONS) MCP MCSE
Nov 21 '05 #6

P: n/a
Darrel,
\\\
Me.Command1.PerformClick()
///


Be aware that with this, you can come in big problems later when you or
somebody else has to change the program.

If somebody has made it temporaly not visible or disabled it, than
perform.click will not fire. Mostly it is not so obvious in code that this
is done in another section.

I hope this helps,

Cor
Nov 21 '05 #7

P: n/a
Herfried,
I recommend to pass the "correct" parameter values in order to prevent
unforseeable problems from occurring if the event handler accesses its
parameter's values in future. To do so, pass a reference to the control
raising the event in the first paramter and 'EventArgs.Empty' in the
second parameter in the case of the 'Click' event.


I think that I have seen this messages from you about 100 times, can you
show us a sample of a future use that can have this effect.

To prevent your showed problem than simple syntax as this in the event is
more than enough (if there is a change that what you wrote can eventually
happen of course).
\\\
If Not Sender is Nothing then
'do every special things that is not general for a raiser of this
routine.
End if
///

I am curious for your sample where this above don't prevent any problem in
future and is not documentive enough?

Cor
Nov 21 '05 #8

P: n/a
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
I recommend to pass the "correct" parameter values in order to prevent
unforseeable problems from occurring if the event handler accesses its
parameter's values in future. To do so, pass a reference to the control
raising the event in the first paramter and 'EventArgs.Empty' in the
second parameter in the case of the 'Click' event.


I think that I have seen this messages from you about 100 times, can you
show us a sample of a future use that can have this effect.

To prevent your showed problem than simple syntax as this in the event is
more than enough (if there is a change that what you wrote can eventually
happen of course).
\\\
If Not Sender is Nothing then
'do every special things that is not general for a raiser of this
routine.
End if
///

I am curious for your sample where this above don't prevent any problem in
future and is not documentive enough?


Have you ever seen somebody checking if 'sender' is 'Nothing'?! If you are
not invoking the event handler maually by passing 'Nothing' in its
parameters, it's guaranteed that 'sender' and 'e' are references to objects.
No need to check if a null-reference has been passed to the method.
Consequently I would not change this behavior. Personally, I consider it
bad style to pass 'Nothing' in the standard event handler's parameters
because it breaks the event handling pattern.

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

Nov 21 '05 #9

P: n/a
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
\\\
Me.Command1.PerformClick()
///


Be aware that with this, you can come in big problems later when you or
somebody else has to change the program.


Mhm... Wouldn't that apply to /every/ method you are using in your code?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #10

P: n/a
>Personally, I consider it bad style to pass 'Nothing' in the standard event
handler's parameters because it breaks the event handling pattern.

Which you are breaking even harder because if the event is not a "click
event" you suggest not to pass nothing. You pretend that you send forever
events and you are only doing that partially. Because that you are than not
consequent is there in my opinion a much harder change on wrong code.

Just my thought,

Cor
Nov 21 '05 #11

P: n/a
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
Personally, I consider it bad style to pass 'Nothing' in the standard
event handler's parameters because it breaks the event handling pattern.

Which you are breaking even harder because if the event is not a "click
event" you suggest not to pass nothing.


That's not true. I simply do not suggest to pass 'EventArgs.Empty' in the
event's second parameter, because sometimes a more specific type is
required.

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

Nov 21 '05 #12

P: n/a
Herfried,
That's not true. I simply do not suggest to pass 'EventArgs.Empty' in the
event's second parameter, because sometimes a more specific type is
required.

This passing of EventArgs.Empty is in my opinion more worse, it suggest that
there is an argument and than it is empty. With 'sender Is nothing' I am
sure that because I use forever the same passing signature in this
situations that there is as well no event.

Although I have showed you that I have used passing of 'me'. This threads
shows me that I should avoid that in future.

Just my thought,

Cor
Nov 21 '05 #13

P: n/a
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
That's not true. I simply do not suggest to pass 'EventArgs.Empty' in
the event's second parameter, because sometimes a more specific type is
required.

This passing of EventArgs.Empty is in my opinion more worse, it suggest
that there is an argument and than it is empty.


For the 'Click' event, the 'e' parameter contains a reference to
'EventArgs.Empty' by default. So passing 'EventArgs.Empty' is OK. For
other event handlers it's better to pass a more appropraite event arguments
object, as I already stated.

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

Nov 21 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.