0

I have simple Windows form With just one button which Calls one url

private void button1_Click(object sender, EventArgs e)
{
    ProcessStartInfo sInfo = new ProcessStartInfo("http://myurl.com/");
    Process.Start(sInfo);
}

When i click exe then it shows button and then i have to click button to perform action. Is it possible to call button click event automatically on clicking exe.

Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
James
  • 1,827
  • 5
  • 39
  • 69

2 Answers2

2

You can do the same with Form.Load event. Just refactor you code with extract method to have no duplicated code and call your function in both event handlers:

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

private void form1_Load(object sender, EventArgs e)
{
    OpenUrl();
}

private void OpenUrl()
{
    ProcessStartInfo sInfo = new ProcessStartInfo("http://myurl.com/");
    Process.Start(sInfo);
}
Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
  • 1
    This is the better answer as it splits the functionality into a well named function. – Steve Feb 10 '16 at 13:41
  • But this still opens the form which is not i want – James Feb 10 '16 at 13:44
  • Just click exe and call url thats it – James Feb 10 '16 at 13:44
  • ok got it.. i just deleted form1 and wrote it in main function – James Feb 10 '16 at 13:46
  • @Happy you can write console application instead of Windows forms app and [hide it](http://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-application) or just wait while it will closed (immediately after `Main()` function executes). – Vadim Martynov Feb 10 '16 at 13:51
0

Simply call:

button1_Click(this, null);

from anywhere in your code.

M. Schena
  • 2,039
  • 1
  • 21
  • 29