Single Instance of a Form
-
I have a Main Application that uses viusal inheritance to call 30 or so other forms. Therefore, I have about 30 dll's that I reference that represent each form. I want to be able to instantiate each form only once. If the form already exists, I want to bring it to focus. I have done this using the following code: private static Namespace.Class Form = null; button1_ClickEvent() { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } This is where my problem arises. I have to write this code for each form I "plan" on instantiating. How could I roll this up into a function or class that I could "call" for each form? For instance: private Instance(Form, Namespace, Class) { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } I would then pass all of the respective variables. Thanks in advance
-
I have a Main Application that uses viusal inheritance to call 30 or so other forms. Therefore, I have about 30 dll's that I reference that represent each form. I want to be able to instantiate each form only once. If the form already exists, I want to bring it to focus. I have done this using the following code: private static Namespace.Class Form = null; button1_ClickEvent() { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } This is where my problem arises. I have to write this code for each form I "plan" on instantiating. How could I roll this up into a function or class that I could "call" for each form? For instance: private Instance(Form, Namespace, Class) { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } I would then pass all of the respective variables. Thanks in advance
You can do that using reflection. But you'd then need to maintain a hashtable of sorts to keep track of which forms are currently visible. If you can do that, then the following code will work.
private Form CreateIfNeeded(Type formType)
{
Form f = (Form) formMap[formType]; //formMap maintains forms which are currently visible.
if (f == null)
{
f = (Form)Activator.CreateInstance(formType);
f.Show();
}
f.BringToFront();
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
I have a Main Application that uses viusal inheritance to call 30 or so other forms. Therefore, I have about 30 dll's that I reference that represent each form. I want to be able to instantiate each form only once. If the form already exists, I want to bring it to focus. I have done this using the following code: private static Namespace.Class Form = null; button1_ClickEvent() { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } This is where my problem arises. I have to write this code for each form I "plan" on instantiating. How could I roll this up into a function or class that I could "call" for each form? For instance: private Instance(Form, Namespace, Class) { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } I would then pass all of the respective variables. Thanks in advance
[Message Deleted]
-
You can do that using reflection. But you'd then need to maintain a hashtable of sorts to keep track of which forms are currently visible. If you can do that, then the following code will work.
private Form CreateIfNeeded(Type formType)
{
Form f = (Form) formMap[formType]; //formMap maintains forms which are currently visible.
if (f == null)
{
f = (Form)Activator.CreateInstance(formType);
f.Show();
}
f.BringToFront();
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Thanks for the response. Why would I need to use the HashTable? The way I had it setup didn't use one. I simply checked whether or not the form was active or not. It seems like there would be an easier way of doing this. I really appreciate your help.
That's because you wanted a generic method to do the job. If you don't have a hashtable, how will you know if a form a has been instantiated or not? In other words, from where will you get the instance of the form to check if it's null or disposed? Regards Senthil _____________________________ My Blog | My Articles | WinMacro