2

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

3
  • 3
    Use the Shown event? (not sure if this is what you mean because your question is unclear) Commented Jul 26, 2013 at 10:16
  • Use Form.Load or Form.Shown event, when that fires, call the same function that your button calls. No need to simulate the click Commented Jul 26, 2013 at 10:16
  • But what do you do in that button click. It is a totally automatic operation (meaning no input is required from the form)? If this is the case then try to refactor your code extracting the code from the button click and call that code from the command arguments passed to your main Commented Jul 26, 2013 at 10:19

4 Answers 4

9

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);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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

Comments

-1

buttonName.PerformClick(); Call the button using PerformClick method

1 Comment

improve your answer by adding more details if solution is also possible using PerformClick to make your answer more useful and understandable.Thanks
-2

you call call button click like this

Button_Click(sender,e)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.