How to raise a click event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a button (btnSearch) and its Click event sub btnSearch_Click.
Clicking the button will trigger the event. But how do I trigger this event
in code? I tried RaiseEvent btnSearch.Click but there is a syntax error.
Or can I just call btnSearch_Click? But what parameters do I pass in?
Thanks.

Li
 
Hi Weng,
You just need to declare the objects that are parameteres to the event.
Here's an example to call Page_Init.

Dim Sndr As System.Object
Dim Evnt As System.EventArgs
Page_Init(Sndr, Evnt)

You might probably want to use a Boolean Session variable to differentiate
if you have forced this event. Set this variable to true before you call the
event function and check it in the called function to understand that you
generated the event.
 
Just put the code inside your click event into another method, and call
that method.

I.e.

Private Sub btn_Click(sender as Object, e as System.EventArgs) Handles
btn.Click
DoSomething()
End Sub

Private Sub DoSomething()

' Put all your button click logic in here

End Sub

Then just call DoSomething() instead of trying to raise the button
click event.
 
Hi Li,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need perform a click on a button in
code. If there is any misunderstanding, please feel free to let me know.

If you're working on a Windows form application, I think you can use
Button.PerformClick method to achieve this. This method generates a Click
event for a button. For more information, please check the following link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformsbuttonclassperformclicktopic.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Li,

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
IMHO I think the *encapsulation* solution offerred by Patrick is the way to
go. It makes for much better manageability in the long run... even if at
first it seems a little inefficient. It's a good practice to adopt.

- What if you want to add a menu that duplicates the button's function?
- What if the code that you want to run is in the MouseMove event? What then?
- What if the button in addition to running the code also closes the form
and you don't want that?

Encapsulate, my friend.
 
Back
Top