James
James

Reputation: 1945

Button click event clicking exe

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.

Upvotes: 0

Views: 282

Answers (2)

Vadim Martynov
Vadim Martynov

Reputation: 8902

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);
}

Upvotes: 2

M. Schena
M. Schena

Reputation: 2107

Simply call:

button1_Click(this, null);

from anywhere in your code.

Upvotes: 0

Related Questions