473,396 Members | 2,004 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,396 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 1645
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.