How to trigger a button in code

  • Thread starter Thread starter Boki
  • Start date Start date
B

Boki

Hi All,

private void ResendTimer_Tick(object sender, EventArgs e)
{
button_Send.Click();

}

I want to trigger a button click event in code.

I think you know this can't pass, need your advice.


Thanks.
Boki.
 
Well, the simplest approach is to just call the same method that the
button-click performs - i.e.

private void button1_Click(object sender, EventArgs e) {
Resend();
}
private void timer1_Tick(object sender, EventArgs e) {
Resend();
}
private void Resend() {
//actual code
}

In this case, both handlers share a signature, so another option would
be to hook both events into the same handler:

private void ResendHandler(object sender, EventArgs e) {
//actual code
}

In some specific cases you can't tell which method(s) is(/are)
handling an event; you can ask the button itself to simulate a click:

private void timer1_Tick(object sender, EventArgs e) {
button1.PerformClick();
}

One of those should do...

Marc
 
Well, the simplest approach is to just call the same method that the
button-click performs - i.e.

        private void button1_Click(object sender, EventArgs e) {
            Resend();
        }
        private void timer1_Tick(object sender, EventArgs e) {
            Resend();
        }
        private void Resend() {
            //actual code
        }

In this case, both handlers share a signature, so another option would
be to hook both events into the same handler:

        private void ResendHandler(object sender, EventArgs e) {
            //actual code
        }

In some specific cases you can't tell which method(s) is(/are)
handling an event; you can ask the button itself to simulate a click:

        private void timer1_Tick(object sender, EventArgs e) {
            button1.PerformClick();
        }

One of those should do...

Marc

It does help, I need the 3rd method, thanks.

Boki.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top