How to Automatically Click a button On a form application?

35,030

Solution 1

The Form.Shown event will fire when the form is shown to the user for the first time, you can enter your event into this

 protected override void OnShown(EventArgs e)
 {
    base.OnShown(e);
    this.BtnClick(null, null);
 }

Solution 2

You can call button click event using Button.PerformClick on Form.Shown event to ensure form is loaded.

private void Form1_Shown(Object sender, EventArgs e) 
{
   Button1.PerformClick(); 
}

The Shown event is only raised the first time a form is displayed; subsequently minimizing, maximizing, restoring, hiding, showing, or invalidating and repainting will not raise this event, Reference

Share:
35,030
akd
Author by

akd

Updated on March 17, 2020

Comments

  • akd
    akd about 4 years

    I have a working form application that has a button on it. I have the source code too. So now I am trying to run the application from schedule tasks on windows and I would like to change the code the to the click button action when the application runs. The problem is the form should be loaded first and then the click action should be fired. Do I need a timer to this? if so could you please give me a help with this? So it takes about 3-4 seconds to load the form application. so it should fire the click action after 5 seconds

  • ρяσѕρєя K
    ρяσѕρєя K over 8 years
    improve your answer by adding more details if solution is also possible using PerformClick to make your answer more useful and understandable.Thanks
  • Vman
    Vman about 6 years
    The Perform Click responses below are better answers IMO. Passing in nulls could be problematic if somebody overrides the event and wants to use sender or EventArgs.