What is the best way to hide the main form on application\'s startup to show it later?
If I just call the Hide
method in the Load
event of this
Modify your Program class - this is where the form is created and shown:
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main () {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 frm = new Form1();
frm.Visible = false;
Application.Run();
}
}
Hopefully your adding some sort of user interface?
The simplest way is to set Opacity = 0
in the designer. Of course you will want to set it back to 100
at some point later..
Or you may want to use a splash screen, maybe like this:
static class Program
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Splash splash = new Splash();
splash.Show();
Application.Run();
}
}
With a splash screen:
public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
}
Form1 form1 = new Form1();
private void Splash_Load(object sender, EventArgs e)
{
form1.WindowState = FormWindowState.Minimized;
form1.Hide();
}
}
You can then show it for example when the splash screen is closed:
private void Splash_FormClosed(object sender, FormClosedEventArgs e)
{
form1.Show();
form1.WindowState = FormWindowState.Normal;
}
Which would happen whenever you want or maybe after some time:
public Splash()
{
InitializeComponent();
Timer timer = new Timer();
timer.Interval = 5000;
timer.Enabled = true;
timer.Tick += (s,e) =>{ this.Close();};
}
Since the program is not watching a Form to close we also need to add this to the main form's closed event:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
If you don't want the splash screen to be visible at all you can hide it like this:
public Splash()
{
InitializeComponent();
this.Opacity = 0;
But please make sure you don't leave the users in the blind: When I start a program I want immediate response!!
You can proceed like this:
private void Form1_Load(object sender, EventArgs e)
{
if (Settings.Instance.HideAtStartup)
{
BeginInvoke(new MethodInvoker(delegate
{
Hide();
}));
}
}
An alternative way can be to use the Application.Run(Form) method. You can create the main form with its Visible property initially set to false, and provide no argument to Application.Run() in main loop.