Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invoke a button click event? 2

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
Is there a way to invoke a command button's Click event? I know this was possible in older versions of VB. Apparently you can't simply go Call CmdBtn.Click() anymore though.

Been looking through help but not finding anything useful, anyone have a tip that may help me?

Thanks



CraigHartz
 
Try

CmdBtn.Click(Nothing, Nothing)

A Click event is looking for two arguments....sender and eventargs. if you are invoking the button from somewhere else, you need to pass nothign to these arguments.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Robert, thanks, that makes sense - but when I type that in I am geting an error stating "Public Event Click is an Event and cannot be called directly. Use RaiseEvent statement to raise an event."

?



CraigHartz
 
Can you post a sample of your code?? I need to see what you are working with.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 

The method of not Click, but PerformClick:

CmdBtn.PerformClick()



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Hehehe...

All this time jebenson and I don't think I have ever leveraged that method. Thank for the tip!

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Outstanding, that worked perfectly. Thanks!

CraigHartz
 
The correct way to call a button click from another button is:

CmdBtn_Click(nothing, nothing) and NOT
CmdBtn.Click(Nothing, Nothing).
IOW, use the _ (underscore)and not a period.
 
In all actuality, it should be whatever the name of the method is...So if the button click method is named as:

Code:
Private Sub ClickMeToExecuteSomeButton(obj As Sender, e As EventArgs) Handles Button1.Click

Then you would call this by: ClickMeToExecuteSomeButton(Nothing, Nothing)

The method for the click of the button can be any Sub as long as it meets the parameter requirements and is assigned with the Handles (or AddHandler).

But to jebenson's post, you can execute the event without passing the Nothing arguments if you leverage the PerformClick event. Much cleaner and easier - I will have to revisit some old projects [smile] !

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 

or, how about this crazy idea.....
Code:
Private Sub Button1_Click(obj As Sender, e As EventArgs) Handles Button1.Click
    Call DoSomething
End Sub

Private Sub Button2_Click(obj As Sender, e As EventArgs) Handles Button2.Click
    Call DoSomething
End Sub

Private Sub DoSomething()
     [green]'Do Something here[/green]
End Sub

Have fun.

---- Andy
 
You can also have multiple handlers and separate with commas!

Code:
Private Sub Button1_Click(obj As Sender, e As EventArgs) Handles Button1.Click, Button2.Click
    Call DoSomething
End Sub

Private Sub DoSomething()
     'Do Something here
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top