how to call button click event

  • Thread starter Thread starter anthonymelillo
  • Start date Start date
A

anthonymelillo

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
 
anthonymelillo said:
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
 
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
 
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

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
 
* "anthonymelillo said:
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.
 
anthonymelillo said:
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. ;-)
 
How would I go about doing that ?

Tony

Herfried K. Wagner said:
You can assign the button to the form's 'AcceptButton' property.

- or -

Raise a click event by calling the button's 'PerformClick' method.
 
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 said:
Seems like a lot of waste duplicating the code in the button click.
 
* "Armin Zingler said:
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.
 
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


Seems like a lot of waste duplicating the code in the button click.
 
Herfried K. Wagner said:
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. ;)
 
you just call the buttons PerformClick method, probably something along the lines of

Button1.PerformClick


anthonymelillo said:
How would I Raise a click event by calling the button's 'PerformClick'
method ?
 
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.
 
* "Tony Vitonis said:
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.
 
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.
 
Back
Top