Is it possibile to create a modal dialog form following the singleton pattern?
The idea is:
public partial class Singleton : Form
{
private static Singleton _instance = null;
private Singleton()
{
// Initialization code
}
public static Singleton Instance
{
get
{
if (_instance == null)
_instance = new Singleton();
return _instance;
}
}
private void Singleton_FormClosing(object sender, FormClosingEventArgs e)
{
_instance.Hide();
e.Cancel = true;
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
This code works fine if the form is non-modal (so, if the Show() method is used), but does not work if the form is modal (so, if the ShowDialog() method is used) because this will also hide the parent form.