How can I hide form1 while Application.Run(form1) is executing?
Application.Run
.
I want to h开发者_运维知识库ide this form (I need it hidden because I run some things in the background, so they must execute) and open another form for a log-in.
The way I am trying this is by executing in my form1 constructor the command this.Hide();
and if log in is successful, show my form1, but it doesn't seem to work. Any ideas?
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.
You can override SetVisibleCore in your Form1 class, to have the Form1 instance hidden on startup. So here is an example which will hide and keep the form hidden, of course you should add some logic that will decide when the form should actually be allowed to become visible.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void SetVisibleCore(bool value)
{
// Quick and dirty to keep the main window invisible
// you can put some logic here to decide when to use the
// incomming value and when to force it to be false as I
// am showing here.
base.SetVisibleCore(false);
}
}
Here is a simple example albeit contrived. Form1 starts invisible and shows Form2, when Form2 is closed it allows Form1 to become visible and shows the form.
using System;
using System.Windows.Forms;
namespace HideMainWinForm
{
public partial class Form1 : Form
{
// Initially the main form cannot show
private bool _canShow = false;
public Form1()
{
InitializeComponent();
Form2 frm = new Form2();
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
frm.Show();
}
void frm_FormClosed(object sender, FormClosedEventArgs e)
{
// Once Form2 is closed we now allow the main form to
// become visible.
_canShow = true;
this.Show();
}
protected override void SetVisibleCore(bool value)
{
base.SetVisibleCore(_canShow && value);
}
}
}
See this : Use the ApplicationContext Class to Fully Encapsulate Splash Screen Functionality
Its basically talks about how you would show a splash form first, then when you've finished loading, how to call your main form. (If you hide the main form while doing something in the background, do consider a splash screen)
Quote from Introduction: This isn't an example on how to create a splash screen for your app. This article explains a clean way to encapsulate splash screen functionality into an inherited ApplicationContext class. This article also shows in detail what happens behind the scenes when a WinForm app starts.
If you need to do some background work before you show your form, I would suggest two possible options.
The first one, is to simply do your background work before you create the form and call Application.Run
on it.
The second option, if you need a message loop to be active while you do the background work, is to use the Application.Run
overload that doesn't take a From as a parameter, and later show your Form exactly when you want to. The only catch is that you will have to cal Application.Exit
or Application.ExitThread
manually when you want to end the application (because it won't have a Form whose Close event it can watch).
So your code could look something like this:
void Main()
{
// trigger some background work
...
// and start the message pump
Application.Run();
}
void SomeBackgroundWork()
{
// let's say now you completed the background work and you want to show your main Form
MyForm f = new MyForm();
f.Close += delegate { Application.Exit(); };
f.Show();
}
Please comment if some clarification is needed...
The problem is the following:
by calling this.Hide()
in the constructor you actually set this.Visible = false
, but immediately after the form instantiation, form.Show is called (by Application.Run
) and form.Show
internally set this.Visible = true
(well, of course is the opposite of Hide...).
You could do in this way:
private bool firstShow = true;
protected override void OnShown(EventArgs e)
{
if (firstShow)
{
this.Hide();
firstShow = false;
return;
}
base.OnShown(e);
}
The form will appear for a moment, and then will disappear immediately.
The boolean flag firstShow
allows you to hide the form only for the first call of Show()
while for the following invocations it will work as usual.
Windows Forms have an event that works perfectly for this called Form.Shown
. It is called only once per Form object when the Form is first shown. Basically it's digEmAll's answer, but already built in.
Add an event handler to the constructor that is called by Form1.Shown
then in the hanlder called Hide()
public form1()
{
InitializeComponent()
this.Shown += new EventHandler(form1_Shown);
// call splash page
}
void form1_Shown(object sender, EventArgs e)
{
this.Hide()
}
You could use this way. However, you must have attention in methods that use await and async.
protected override void OnVisibleChanged(EventArgs e){
base.OnVisibleChanged(e);
this.Visible = false;
}
Methods that use (await and async) must have the word (result) in the form initialization.
Example:
public frmForm1() {
InitializeComponent();
try {
if (Program.LaunchedViaStartup == true) {
var varclsReturnBack = funcStartBackGroundObjectsAsync().Result;
}
} catch (Exception varException) {
Console.Write(varException.Message);
}
}
精彩评论