Click here to Skip to main content
15,914,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I have all the form name in my application and What i have to do is To get the instance of that form

All forms name are available in the database and selected named form will be open


So I have just string variable of that form and I have to get the instance of that form


So please Help me for this problem.

Thanks
Posted

This discussion [^]on DaniWeb may be useful. It boils down to this.

C#
public void Open_Form(string formname)
{

    Type type = Type.GetType("myProject.Forms." + formname);
    object obj = Activator.CreateInstance(type);
    (obj as Form).MdiParent = this;
    (obj as Form).Show();

}
 
Share this answer
 
I thing for that you have to use reflection of .Net

may this helps you
 
Share this answer
 
The first thing you need to do is to keep a collection of open forms somewhere in your application that you can perform the lookup against.
Then you can simply iterate over the collection checking the name...
C#
public Form GetForm(string name)
{
    Form result = null;
    foreach(Form form in formsCollection)
    {
        if(form.Name == name)
        {
            result = form;
            break;
        }
    }
    return result;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900