Reputation: 393
Problem: Executing the code below with Application.Run() with no parameters displays nothing.
Background Information: I have a WinForms application that I'm running and I want the default form to be a singleton, and not show up when I first run Application.Run (I want full control of when to show it).
In my Program.cs, using
Application.Run(Form1.Instance)
works perfectly, but I'm using Application.Run() with no parameters followed by Form1.Instance.Show() so that I can control when to hide and show my Form1.
What am I missing here?
Program.cs:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run();
Form1.Instance.Show();
}
}
Form1.cs:
public partial class Form1 : Form
{
private static Form1 instance;
public Form1()
{
InitializeComponent();
}
public static Form1 Instance
{
get
{
if (instance == null)
{
instance = new Form1();
}
return instance;
}
}
}
Upvotes: 0
Views: 1264
Reputation: 4683
put Form1.Instance.Show(); before Application.Run();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1.Instance.Show();
Application.Run();
and handle formclosed event of Form1 t oforce application to exit
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { Application.Exit(); }
Upvotes: 4