Monday, March 22, 2010

Opening Single Window at MDI Form C#

This simple example codes will open a single window. I have 2 ways of opening a window in MDI Form in C#.


Creating a Instance in a Form
Just create a variable of a Form

First
Create a MDI Form and a Form1.

In Form 1 write This code
private static Form1 sForm = null;
public static Form1 Instance()
{
if (sForm == null)
{ sForm = new Form1 (); }
return sForm;
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
sForm = null;
}


and In mdi Form write This code
Form sForm = new Form1.Instance();
sForm.MdiParent=this;
sForm.show();
sForm.Activate();


Example 2:

-Create a MDI Form and a Form1.

*Write this codes at your MDI Form:

//Create a variable:
private Form1 frm1 = new Form1();
//Button showing a Form1:
frm1.MdiParent=this;
frm1.show();