tags:

views:

38

answers:

2

I wanted to hide the main window of my app on startup, so I put this in the constructor:

this.Hide();

This doesn't hide my form though. It seems like I can only get buttons to hide the form. Am I doing something wrong here?

+1  A: 

Try setting the visible property of the form to false before it is loaded in the main entry point of your application.

Form1 obj = new Form1();
obj.visible = false;
Application.Run(obj);

Or try setting the co-ordinates of the form to higher location like 9000, 9000.

Sidharth Panwar
+1  A: 

you can use this line of code. It wont hide it, but it will be minimized:

this.WindowState = FormWindowState.Minimized;

in addition, if you don't want it showing on the task bar either, you can add this line:

this.ShowInTaskbar = false;

But why do you create the form if you don't want it to be visible in the first place?

Øyvind Bråthen
Because it will start with Windows and reside in the system tray in a way similar to antivirus and firewall software. Any idea as to why `this.Hide()` doesn't actually hide the form when called from the form constructor?
Pieter
I think this is because at this point (when running the constructor) your form has not been made visible yet. Only after the constructor is done, the form will be created and made visible. So your only problem why this does not work is because it's done from the constructor. I don't suggest this as a solution, but as an experiment, make a timer and start it from the constructor with a 1 second delay, and have the timer_tick method hide the form. This will work, since at the time you call this.Hide() the form will be visible.
Øyvind Bråthen
You get the same problem when you have a construct like this in program.cs: Application.Run(new Form1()); and you try to write Application.Exit() in the constructor of Form1. After the Application.Exit() call your application will still be alive and well, and that is because the constructor is run before Application.Run, and thereby will have no effect. So same problem for you. You make it Hidden, just to have Application.Run make it vivible and kicking again.
Øyvind Bråthen
I went with your solution, but I also attached a method to the form's `Load` event so that I can hide the window properly using `this.Hide()` once the form has been loaded. Thanks!
Pieter
Form.Load will be a good place for this probably. One questions since I haven't tried myself. Does the form first appear, and then get hidden so you experience a flicker if you hide it from the Load event if you don't minimize it in the constructor?
Øyvind Bråthen