0

I have created a simple C# application to automatically login into a hotspot. I have a notify icon with a contextmenustrip with some functions like Connect, Disconnect, etc. I want to be able to run the form in the background showing only the notify icon to automatically login into the hotspot if the form is hidden.

I followed the instructions from VBNight in this post: Hide form at launch

The application is running in the background, the notify icon showing but the Form_Load function is not working until I press on the notify icon.

Community
  • 1
  • 1
Sergio
  • 31
  • 5
  • *I would like to run the executable with a parameter to hide the form.*. Ok, What is the problem? – Sriram Sakthivel Oct 17 '14 at 07:10
  • That code accomplishes nothing, the form's Show() method is never called. You can't get the program stopped either by calling the form's Close() method. You'll have to cut the Load event umbilical cord, initialization code belongs in the constructor. Check [this post](http://stackoverflow.com/a/1732294/17034) for an alternative. – Hans Passant Oct 17 '14 at 13:23

2 Answers2

1

I guess that's because Form_Load Occurs before a form is DISPLAYED for the first time.

Try moving your code from Form_Load to the constructor after InitializeComponent(); or so.


EDIT:

To answer your question, I suggest you extract code #1 from...

private void YOUR_BUTTON_Click(object sender, EventArgs e) {
    // move this code #1 to...
}

and move the code to a brandnew method.

private void NewButtonClicked() {
    // move code #1 here (in case)
}

Then, go back and call the method you just created.

private void YOUR_BUTTON_Click(object sender, EventArgs e) {
    // You can leave code #1 but to remove duplicate,
    NewButtonClicked();
}

Finally, replace YOUR_BUTTON.PerformClick(); with NewButtonClicked(); wherever you need. I assume you don't need any interaction with form Controls since the form is hidden.

Attacktive
  • 604
  • 1
  • 11
  • 25
  • That works. I can place code there. But I cannot use the .PerformClick() because the form is not loaded. – Sergio Oct 17 '14 at 07:53
1

I fixed it changing the form opacity to 0 and the ShowInTaskbar property to false. The form is hidden and the code is working.

Sergio
  • 31
  • 5