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

how to call button click event

P: n/a
Is there a way to call a button click event from somewhere else ?

I have a text box where I would like to call a button click event when a
user presses enter in the text box.

Can I do this ?
Thanks
Tony
Nov 20 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
"anthonymelillo" <no************@earthlink.net> schrieb
Is there a way to call a button click event from somewhere else ?

I have a text box where I would like to call a button click event
when a user presses enter in the text box.

Can I do this ?


If you want the button to be pressed anywhere in the Form when enter is
pressed, set the Form's Acceptbutton property = Button
--
Armin

Nov 20 '05 #2

P: n/a
That seems to work great. But is there any way I can call a button event if
I want to use it elsewhere ?

Thanks
Tony

"Armin Zingler" <az*******@freenet.de> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
"anthonymelillo" <no************@earthlink.net> schrieb
Is there a way to call a button click event from somewhere else ?

I have a text box where I would like to call a button click event
when a user presses enter in the text box.

Can I do this ?


If you want the button to be pressed anywhere in the Form when enter is
pressed, set the Form's Acceptbutton property = Button
--
Armin

Nov 20 '05 #3

P: n/a
Don

I've never liked to see code that calls Button_Click, etc. directly. I
think it's better practice to put the code in a procedure that can
then be called by a button or wherever it's needed.

Sub Button_Click()
DoIt
End Sub

Sub DoIt()
'--Do it here
End Sub

Don
Redmond, WA

On Wed, 8 Oct 2003 19:31:23 -0400, "anthonymelillo"
<no************@earthlink.net> wrote:
That seems to work great. But is there any way I can call a button event if
I want to use it elsewhere ?

Thanks
Tony

"Armin Zingler" <az*******@freenet.de> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
"anthonymelillo" <no************@earthlink.net> schrieb
> Is there a way to call a button click event from somewhere else ?
>
> I have a text box where I would like to call a button click event
> when a user presses enter in the text box.
>
> Can I do this ?


If you want the button to be pressed anywhere in the Form when enter is
pressed, set the Form's Acceptbutton property = Button
--
Armin


Nov 20 '05 #4

P: n/a
* "anthonymelillo" <no************@earthlink.net> scripsit:
Is there a way to call a button click event from somewhere else ?

I have a text box where I would like to call a button click event when a
user presses enter in the text box.


You can assign the button to the form's 'AcceptButton' property.

- or -

Raise a click event by calling the button's 'PerformClick' method.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
"anthonymelillo" <no************@earthlink.net> schrieb
That seems to work great. But is there any way I can call a button
event if I want to use it elsewhere ?


I agree with "Don" an don't give you the suggestion to call
Button1.PerformClick. ;-)
--
Armin

Nov 20 '05 #6

P: n/a
Seems like a lot of waste duplicating the code in the button click.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:e0*************@tk2msftngp13.phx.gbl...
"anthonymelillo" <no************@earthlink.net> schrieb
That seems to work great. But is there any way I can call a button
event if I want to use it elsewhere ?


I agree with "Don" an don't give you the suggestion to call
Button1.PerformClick. ;-)
--
Armin

Nov 20 '05 #7

P: n/a
How would I go about doing that ?

Tony

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
* "anthonymelillo" <no************@earthlink.net> scripsit:
Is there a way to call a button click event from somewhere else ?

I have a text box where I would like to call a button click event when a
user presses enter in the text box.


You can assign the button to the form's 'AcceptButton' property.

- or -

Raise a click event by calling the button's 'PerformClick' method.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #8

P: n/a
except that you would then also only call the DoIt function from your button click event as well.
Then you only have the code in one place, but it can be called from anywhere.
"anthonymelillo" <no************@earthlink.net> wrote in message
news:e6*************@TK2MSFTNGP12.phx.gbl...
Seems like a lot of waste duplicating the code in the button click.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:e0*************@tk2msftngp13.phx.gbl...
"anthonymelillo" <no************@earthlink.net> schrieb
That seems to work great. But is there any way I can call a button
event if I want to use it elsewhere ?


I agree with "Don" an don't give you the suggestion to call
Button1.PerformClick. ;-)
--
Armin


Nov 20 '05 #9

P: n/a
* "Armin Zingler" <az*******@freenet.de> scripsit:
That seems to work great. But is there any way I can call a button
event if I want to use it elsewhere ?


I agree with "Don" an don't give you the suggestion to call
Button1.PerformClick. ;-)


Why not?! If there are only some lines of code in the button's 'Click'
event handler it IMO doesn't make sense to put it into a separate procedure.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #10

P: n/a
* "anthonymelillo" <no************@earthlink.net> scripsit:
How would I go about doing that ?


What?

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #11

P: n/a
Don

Hi Tony,

The point I was trying to make is that it's easier on yourself (and
those that might end up having to maintain your code) if you don't put
code directly in your form's or control's event handlers. I believe
your code will be much more readable and easier to plan and maintain
if you put your code in a well thought out class structure with very
little code in your forms.

Instead of putting a lot of code to populate a listview control with
customer information in your button_click, put the code in your
Customer class and then simply call it from your button or any other
place that it's needed.

Sub btnFillCustomerList( )
oCustomer.FillListview(lvCustomer)
End Sub

This avoids the duplication ( the code is in one logical location )
that you mentioned and allows your code to be arranged in logical
units which are easier to understand and maintain .

Cheers,

Don
Redmond, WA
On Thu, 9 Oct 2003 13:05:01 -0400, "anthonymelillo"
<no************@earthlink.net> wrote:
Seems like a lot of waste duplicating the code in the button click.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:e0*************@tk2msftngp13.phx.gbl...
"anthonymelillo" <no************@earthlink.net> schrieb
> That seems to work great. But is there any way I can call a button
> event if I want to use it elsewhere ?


I agree with "Don" an don't give you the suggestion to call
Button1.PerformClick. ;-)
--
Armin


Nov 20 '05 #12

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
* "Armin Zingler" <az*******@freenet.de> scripsit:
That seems to work great. But is there any way I can call a
button event if I want to use it elsewhere ?


I agree with "Don" an don't give you the suggestion to call
Button1.PerformClick. ;-)


Why not?! If there are only some lines of code in the button's
'Click' event handler it IMO doesn't make sense to put it into a
separate procedure.


Matter of taste. A click is done by the user - unless you're using a
force-feedback mouse. ;)
--
Armin

Nov 20 '05 #13

P: n/a
How would I Raise a click event by calling the button's 'PerformClick'
method ?

--
Tony

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
* "anthonymelillo" <no************@earthlink.net> scripsit:
How would I go about doing that ?


What?

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #14

P: n/a
you just call the buttons PerformClick method, probably something along the lines of

Button1.PerformClick
"anthonymelillo" <no************@earthlink.net> wrote in message
news:Of*************@TK2MSFTNGP11.phx.gbl...
How would I Raise a click event by calling the button's 'PerformClick'
method ?

--
Tony

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
* "anthonymelillo" <no************@earthlink.net> scripsit:
How would I go about doing that ?


What?

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>


Nov 20 '05 #15

P: n/a
* "anthonymelillo" <no************@earthlink.net> scripsit:
How would I Raise a click event by calling the button's 'PerformClick'
method ?


Just call 'Button1.PerformClick()'.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #16

P: n/a
* "Armin Zingler" <az*******@freenet.de> scripsit:
> That seems to work great. But is there any way I can call a
> button event if I want to use it elsewhere ?

I agree with "Don" an don't give you the suggestion to call
Button1.PerformClick. ;-)


Why not?! If there are only some lines of code in the button's
'Click' event handler it IMO doesn't make sense to put it into a
separate procedure.


Matter of taste. A click is done by the user - unless you're using a
force-feedback mouse. ;)


:-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #17

P: n/a
One objection might be that the typical event handler gets two
parameters, a System.Object and a System.EventArgs, and some callers
might not have access to instances of those things to pass along.
Herfried K. Wagner wrote...

Why not?! If there are only some lines of code in the button's
'Click' event handler it IMO doesn't make sense to put it into a
separate procedure.
anthonymelillo wrote...

I agree with "Don" an don't give you the suggestion to call
Button1.PerformClick. ;-)
Armin Zingler wrote...

That seems to work great. But is there any way I can call a button
event if I want to use it elsewhere ?

Nov 20 '05 #18

P: n/a
* "Tony Vitonis" <no***@nowhere.com> scripsit:
One objection might be that the typical event handler gets two
parameters, a System.Object and a System.EventArgs, and some callers
might not have access to instances of those things to pass along.


When calling 'PerformClick', you don't need to know those things.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #19

P: n/a
That's true. The PerformClick method probably has to instantiate the
two objects, then, and call the event handler. A separate routine
would eliminate the objects and the extra call. On the other hand,
there'd be an extra call in the button click event.

I guess it depends, just like everything else in life.
Herfried K. Wagner wrote...

When calling 'PerformClick', you don't need to know those things.
Tony Vitonis scripsit:

One objection might be that the typical event handler gets two
parameters, a System.Object and a System.EventArgs, and some
callers might not have access to instances of those things to pass
along.

Nov 20 '05 #20

P: n/a
* "Tony Vitonis" <no***@nowhere.com> scripsit:
That's true. The PerformClick method probably has to instantiate the
two objects, then, and call the event handler. A separate routine
would eliminate the objects and the extra call. On the other hand,
there'd be an extra call in the button click event.


As mentioned previously, I use 'PerformClick' if it is semantically
correct and I don't use it if it is not correct.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.