Click here to Skip to main content
15,922,533 members
Home / Discussions / C#
   

C#

 
Questionhow to click a button Pin
imran_rafique9-Aug-02 21:02
imran_rafique9-Aug-02 21:02 
Questionhow to click a button Pin
imran_rafique9-Aug-02 10:47
imran_rafique9-Aug-02 10:47 
AnswerRe: how to click a button Pin
Nick Parker9-Aug-02 10:52
protectorNick Parker9-Aug-02 10:52 
GeneralRe: how to click a button Pin
Rama Krishna Vavilala9-Aug-02 12:07
Rama Krishna Vavilala9-Aug-02 12:07 
GeneralRe: how to click a button Pin
Nick Parker9-Aug-02 13:51
protectorNick Parker9-Aug-02 13:51 
GeneralRe: how to click a button Pin
Rama Krishna Vavilala9-Aug-02 15:36
Rama Krishna Vavilala9-Aug-02 15:36 
GeneralRe: how to click a button Pin
Nick Parker9-Aug-02 16:31
protectorNick Parker9-Aug-02 16:31 
GeneralRe: how to click a button Pin
James T. Johnson9-Aug-02 17:00
James T. Johnson9-Aug-02 17:00 
Perhaps a fresh perspective is needed Wink | ;)

When dealing with events, two things are involved. A multicast delegate (ie a delegate that when invoked runs multiple methods) and the event handlers attached to that delegate.

When one calls myButton.PerformClick() you are telling the Button to fire the Click event, this in turn invokes the multicast delegate that represents the event object. This will then run all event handlers attached to the event/delegate.

When you call myButton_Click(this, EventArgs.Empty); you are calling a method that just happens to be attached to the Click event. None of the other handlers get fired and thus the click didn't really happen to anything but that one handler.

A simple test, create a form with a button named "myButton" on it, now add the following code to the class

protected override void OnLoad(EventArgs e)
{
  myButton.Click += new EventHandler(click_a);
  myButton.Click += new EventHandler(click_b);
}
 
private void click_a(object sender, EventArgs e)
{
  MessageBox.Show("In click_a");
} 
 
private void click_b(object sender, EventArgs e)
{
  MessageBox.Show("In click_b");
}
Now add another button to the form and try your way of forcing a click and the PerformClick method.

Obviously you'll have to call both click_a and click_b in order to get the same effect; but what if other objects have attached themselves to the click event? You have no direct way of calling them since you don't even know who or what they are; which is why the PerformClick method was created.

Hopefully that makes it clearer for you Wink | ;)

James
GeneralRe: how to click a button Pin
Nick Parker9-Aug-02 17:24
protectorNick Parker9-Aug-02 17:24 
GeneralRe: how to click a button Pin
James T. Johnson9-Aug-02 18:32
James T. Johnson9-Aug-02 18:32 
GeneralRe: how to click a button Pin
Nick Parker9-Aug-02 18:39
protectorNick Parker9-Aug-02 18:39 
AnswerRe: how to click a button Pin
Rama Krishna Vavilala9-Aug-02 12:06
Rama Krishna Vavilala9-Aug-02 12:06 
GeneralControlling the Mouse pointer Pin
albean9-Aug-02 9:42
albean9-Aug-02 9:42 
GeneralRe: Controlling the Mouse pointer Pin
Nick Parker9-Aug-02 10:57
protectorNick Parker9-Aug-02 10:57 
GeneralRe: Controlling the Mouse pointer Pin
albean9-Aug-02 11:12
albean9-Aug-02 11:12 
GeneralRe: Controlling the Mouse pointer Pin
Nick Parker9-Aug-02 11:17
protectorNick Parker9-Aug-02 11:17 
GeneralRe: Controlling the Mouse pointer Pin
imran_rafique13-Aug-02 9:50
imran_rafique13-Aug-02 9:50 
GeneralRe: Controlling the Mouse pointer Pin
Nick Parker13-Aug-02 9:54
protectorNick Parker13-Aug-02 9:54 
GeneralRe: Controlling the Mouse pointer Pin
imran_rafique13-Aug-02 10:13
imran_rafique13-Aug-02 10:13 
GeneralRe: Controlling the Mouse pointer Pin
Nick Parker13-Aug-02 10:59
protectorNick Parker13-Aug-02 10:59 
QuestionTransparent color? Pin
Rickard Andersson209-Aug-02 6:12
Rickard Andersson209-Aug-02 6:12 
AnswerRe: Transparent color? Pin
Anonymous9-Aug-02 7:06
Anonymous9-Aug-02 7:06 
AnswerRe: Transparent color? Pin
Nick Parker9-Aug-02 11:00
protectorNick Parker9-Aug-02 11:00 
AnswerRe: Transparent color? Pin
James T. Johnson9-Aug-02 15:21
James T. Johnson9-Aug-02 15:21 
GeneralThe Wierdest Think Ever! Pin
Joel Matthias9-Aug-02 5:28
Joel Matthias9-Aug-02 5:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.