Mr Kumar,
How about something like the following:
Code:
public partial class MyDialog : Form
{
//------------------------------
// Static Member Variables
//------------------------------
public static int InstanceCount = 0;
//------------------------------
// Constructors
//------------------------------
public MyDialog()
{
InitializeComponent();
InstanceCount++;
}
//------------------------------
// Overrides
//------------------------------
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
InstanceCount = Math.Max(InstanceCount - 1, 0);
System.Diagnostics.Debug.WriteLine(string.Format("Disposing of a MyDialog object. Instance count = {0}", InstanceCount));
base.Dispose(disposing);
}
}
//========================================
public partial class MainForm : Form
{
//------------------------------
// Member Variables
//------------------------------
protected MyDialog m_myDialog = null;
//------------------------------
// Constructors
//------------------------------
public MainForm()
{
InitializeComponent();
}
//------------------------------
// Overrides
//------------------------------
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
System.Diagnostics.Debug.WriteLine("MainForm double-clicked...");
if (MyDialog.InstanceCount == 0)
{
System.Diagnostics.Debug.Write("Instantiating a new instance of MyDialog...");
try
{
m_myDialog = new MyDialog();
m_myDialog.Show();
System.Diagnostics.Debug.WriteLine(string.Format("instance #{0} created.", MyDialog.InstanceCount));
}
catch
{
if (m_myDialog != null)
m_myDialog.Dispose();
System.Diagnostics.Debug.WriteLine("failed.");
}
}
else
System.Diagnostics.Debug.WriteLine("Choosing NOT to instantiate a new instance of MyDialog.");
}
//------------------------------
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if ((!e.Cancel) && (m_myDialog != null))
{
m_myDialog.Dispose();
m_myDialog = null;
}
}
}
Cheers.
- Roger Nedel