473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to raise a click event

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

Jul 21 '05 #1
6 1642
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.

"Li Weng" wrote:
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

Jul 21 '05 #2
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.

Jul 21 '05 #3
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/de...us/cpref/html/
frlrfsystemwindowsformsbuttonclassperformclicktopi c.asp

HTH.

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

Jul 21 '05 #4
Thanks to Cherry, Patrick and Kevin for your help!

Li
"Kevin Yu [MSFT]" wrote:
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/de...us/cpref/html/
frlrfsystemwindowsformsbuttonclassperformclicktopi c.asp

HTH.

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

Jul 21 '05 #5
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."

Jul 21 '05 #6
CMM
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.

"Li Weng" wrote:
Thanks to Cherry, Patrick and Kevin for your help!

Li
"Kevin Yu [MSFT]" wrote:
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/de...us/cpref/html/
frlrfsystemwindowsformsbuttonclassperformclicktopi c.asp

HTH.

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

Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: asr | last post by:
I've a form with a button on it. How to raise (I mean to run the code within the click event of the button) without clicking the button? Is it possible to raise the events programatically.
2
by: (Pete Cresswell) | last post by:
I've been perusing a "real-life" application and notice that for instance, in a button's Click() event, they don't write the processing code. Instead, they raise an event like AddNewRecord and...
3
by: Cary Linkfield | last post by:
I have a standard form I use. It inherits from Windows.Forms.Form. I usually add a Cancel button the form in the designer. I want to raise the Cancel button's Click event when the user presses...
2
by: Oney | last post by:
I want to open a popup window when user click the web server button control When user click the button, only OnClik="OpenWindow(12)" is sent but no event happend no popup opened.After...
3
by: moondaddy | last post by:
I wrote my own List class which I use to bind to list controls. this class inherits CollectionBase and implements IBindingList. This class contains a list of business classes such as customers...
6
by: Li Weng | last post by:
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...
2
by: Dave | last post by:
Hello, I'm starting with asp.net and i don't know how to raise the SelectedIndexChange event when clicking in a dropdownlist control. I thought it was the same as by clicking on a button. ...
0
by: prashantnurvi | last post by:
I have programaticaaly created several Link Buttons on my ASPX page, I would like to raise the Click event of these Link buttons, passing in appropriate parameters depending on the Link button I...
5
by: Mike | last post by:
Hi group; Let say I have an object called Account, that raises an event called AccountLow with its owns EventArgs, and when this event gets raised, I will like to raise another custom...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.