views:

1233

answers:

5

I am new to C# and am creating a C# Windows Mobile application that I need to programmatically invoke the click event on a Button. I have looked at the Button class and do not see a way to do this.

Any ideas?

Zzub

+8  A: 

You might consider changing your design: at least move the logic from button1_Click handler somewhere else so that you'll be able to invoke it from wherever you want.

Anton Gogolev
Unluckily this seems the only answer for the Windows Mobile platform. I wish MS would add something that would allow this.Zzub
I cannot think of any sane reason why anybody would want to click buttons programatically.
Anton Gogolev
If you have a hardware button that you want to click the button. That seems like an obvious one.
ctacke
A: 

Anything that prevents you from simply invoking the method that handles the onClick event?

Sergio
Why the down vote? I can read some other answers that say the same thing and don't have down votes...
Sergio
Yea, I don't get the downvote either. Although, I wouldn't advise calling the button1_Click event directly. Something about it just doesn't feel "clean" to me. +1 to balance out though.
BFree
@Sergio: I haven't voted you down, but your answer can be read with a very nasty "Why haven't you thought of the easiest thing?" way, which is not very "helpful", which is what a vote measures.
David Schmitt
Sorry if i sounded nasty, that was not my intention... What i really meant was if there was anything that would prevent him from doing that, like company policies, etc.. And at the same time point out a way to do this.
Sergio
+2  A: 

On the full blown .NET side you can call:

this.button1.PerformClick();

Not sure if that works on WindowsMobile. You're other option is to do something like this:

    private void button1_Click(object sender, EventArgs e)
    {
        OnButtonClick();
    }

    private void OnButtonClick()
    {

    }

Then you can call your OnButtonClick() wherever you need to.

BFree
Nope, PerformClick doesn't exist in the CF.
ctacke
button1_Click is still just a method. You can call it directly.
Joel Coehoorn
Yes, you CAN, but I think it's a lot cleaner to have a method that describes what you want to to, rather than calling button1_Click(null,new EventArgs());
BFree
+1  A: 

Not exactly the answer you were probably looking for:::

You might just call the code you want to run, move the meat of the code outside the OnClick handling method. The OnClick method could fire that method the same as your code.

Greg Ogle
A: 

He wants the button graphic to move as well, just as if the mouse was clicked on it, however the mouse was not actually clicked on it. Do not question his motivation, or suggest that you cant think of any reason it should be done, just tell him how to do it. Perhaps he has an external button connected via a digital input (usb based interface card), this button must act just like a left button click whilst also allowing the mouse to work, in this case it would be nice to trigger a mouse click method whenever he d*amn well wants. And see the button graphic move down, not just call some lame method that is invoked by the button click event.

Richard