I wanted to hide the main window of my app on startup, so I put this in the constructor:开发者_如何学Go
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?
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?
Just override the OnVisibleChanged method and change the visibility of the form in there, something like this:
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
this.Visible = false;
}
And that's it! Simple and clean.
If you would rather use this.Hide or this.Show you can do this
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.Hide();
}
I tried to do this by setting Visible to false or hiding in the constructor and in the OnLoad event.
Neither of these had any effect, as the form is set to Visible after the form is created and after the OnLoad event is fired, in SetVisibleCore.
Setting the form to hidden in the Shown event works, but the form flickers on the screen for a moment.
You can also override the SetVisibleCore and set the value to false, but then OnLoad isn't fired and some of the other events are messed up, such as form closing.
The best solution in my opinion is to set the form to minimised and not shown in the taskbar before calling Application.Run().
So instead of:
Application.Run(new MainForm());
do:
MainForm form = new MainForm();
form.WindowState = FormWindowState.Minimized;
form.ShowInTaskbar = false;
Application.Run(form);
Then the application will run with all the proper events fired (even OnShown) and the form will not be displayed.
If you want to be able to hide / show the form like normal after that, then you need to set the WindowState and ShowInTaskbar back to Normal and true.
In the Shown event, you can set ShownInTaskbar back to true and then properly hide the form.
this.Shown += new System.EventHandler(this.MainFormShown);
...
void MainFormShown(object sender, EventArgs e)
{
this.ShowInTaskbar = true;
this.Visible = false;
}
Settings the WindowState back to Normal whilst the form is hidden has no effect, so you will need to do it after you show the form again, otherwise the icon will be in the taskbar but the form will be minimised.
this.Show();
this.WindowState = FormWindowState.Normal;
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.
You can start a form hidden using Form.Hide()
:
Form2 form = new Form2();
//Start Form2 hidden
form.Hide();
//Close Form2
form.Close();
You can create a firstOpen bool variable in the form and make this in the Show event
private void Form1_Shown(object sender, EventArgs e)
{
if (firstOpen)
{
this.WindowState = FormWindowState.Minimized;
Hide();
notifyIcon1.Visible = true;
}
firstOpen = false;
}
In the first time it is showed, it will be hidded. Next time will works normal.
To hide forms on start up, it's actually very simple. You may create the form and store it in a local variable, and simply does not put the form in Application.Run()
so that it is not rendered on startup:
Form1 form = new Form1();
Application.Run();
Øyvind is right on that calling this.Hide()
from the constructor would not work because the form is not ready yet. In this way, since the form is already created, you may call this.Show()
in any function of the form, and call this.Hide()
if you want to hide it.
精彩评论