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.
Further to my comment, don’t do it this way. Don’t make the form/dialog a singleton. The dialog should just present a view of the data you want to show. The caching of the data should be handled elsewhere1. So, when you create the dialog, pass it the cached objects you want it to show. Essentially, use an MVC pattern.