Kreij
Senior Monkey Moderator
- Joined
- Feb 6, 2007
- Messages
- 13,817 (2.07/day)
- Location
- Cheeseland (Wisconsin, USA)
Ok, if you read my post on Single instance forms (if you haven't read that post its HERE), you are probably thinking, "Kreij, I have a launcher bar with ten buttons that open ten forms. That would be a buttload of redundant coding !! Isn't there a better way ?"
Well there sure is a better way. Use Generics.
Generics let you create classes and methods that take variable Types.
Variable type classes are declared like this ... class Class1<T>
Where the "T" will be replaced by the type you instantiate the class with (ie. string, int, Form, etc).
It can also take qualifiers for the class types (ie. Form, new() )
So the class declaration "class Class1<T> where T : Form, new()" tells the compiler that the "T" must be of a "Form" type that has its own constructor (the new())
So what do you do ...
First create a class that will handle generic forms
Now instead of having to modify each form's codefile to make sure it handles singular instances, all you have to do is call the generic class and it will make sure the single instance is managed and call the form constructor for you.
In the following example, I have created a form (Form1) that has 2 buttons (button1 and button2).
Each button opens another form (Form2 and Form3).
I did not include the code for Form2 and Form3 as they are just empty forms with no controls or anything. This is just proof of concept coding
As you can see, from your main form you do not have to instantiate the other forms or call any methods or properties on the forms to get the same functionality as my initial post on single form instances. You just have to create an instance of the generic class for any form you want to make.
I gotta admin, C# has some pretty slick features ...
Comments or questions are welcome
Edit :
Oops, I forgot about a little clean up ...
In your form code files that the generic class calls you still must have a way of nulling out the "mInst" value so that the generic class can recreate the form if you have closed it.
So, without ado, here is the OnClose call for "Form2" ...
Well there sure is a better way. Use Generics.
Generics let you create classes and methods that take variable Types.
Variable type classes are declared like this ... class Class1<T>
Where the "T" will be replaced by the type you instantiate the class with (ie. string, int, Form, etc).
It can also take qualifiers for the class types (ie. Form, new() )
So the class declaration "class Class1<T> where T : Form, new()" tells the compiler that the "T" must be of a "Form" type that has its own constructor (the new())
So what do you do ...
First create a class that will handle generic forms
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
class Class1<T> where T : Form, new()
{
private static T mInst;
public Class1()
{
// Here i am just stuffing our single form instance code
// into the class constructor
if (mInst == null)
{
mInst = new T();
mInst.Show();
}
else
{
mInst.WindowState = FormWindowState.Normal;
mInst.Focus();
}
}
// You need this method for when a form closes
public void letGo()
{
mInst = null;
}
}
}
Now instead of having to modify each form's codefile to make sure it handles singular instances, all you have to do is call the generic class and it will make sure the single instance is managed and call the form constructor for you.
In the following example, I have created a form (Form1) that has 2 buttons (button1 and button2).
Each button opens another form (Form2 and Form3).
I did not include the code for Form2 and Form3 as they are just empty forms with no controls or anything. This is just proof of concept coding

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// create an instance of class1 that will create a Form2 instance
Class1<Form2> temp = new Class1<Form2>();
}
private void button2_Click(object sender, EventArgs e)
{
// create an instance of class1 that will create a Form3 instance
Class1<Form3> temp = new Class1<Form3>();
}
}
}
As you can see, from your main form you do not have to instantiate the other forms or call any methods or properties on the forms to get the same functionality as my initial post on single form instances. You just have to create an instance of the generic class for any form you want to make.
I gotta admin, C# has some pretty slick features ...
Comments or questions are welcome

Edit :
Oops, I forgot about a little clean up ...
In your form code files that the generic class calls you still must have a way of nulling out the "mInst" value so that the generic class can recreate the form if you have closed it.
So, without ado, here is the OnClose call for "Form2" ...
Code:
protected override void OnClosed(EventArgs e)
{
Class1<Form2> temp = new Class1<Form2>();
temp.letGo();
base.OnClosed(e);
}
Last edited: