Hi

I would like to create a method for opening up a form by passing across a string.

e.g. Something along the lines of the below code.
Is this possible in C#?

Public void OpenForm (string formname)
{

formname myform = new formname();
myform.show();

}

Recommended Answers

All 9 Replies

Type type = Type.GetType("WindowsFormsApplication1.Form2");
            object obj = Activator.CreateInstance(type);
            (obj as Form).Show();

I am not sure how to get this going.
I am passing across the form name as a string. when I run the code the value of type is null.

I have even hard coded the form name and type still comes up as null.

Am I missing something

public void Open_Modal_Form(string formname)
        {

            Type type = Type.GetType(formname); // This == null
            object obj = Activator.CreateInstance(type);
            (obj as Form).Show();

        }

You need a fully qualified name.

"Namespace.Class"

You're a legend!!! Thanks for the help :-)

ok I thought that was the end to my solution but I still have a small issue.

The form opens outside of the MainForm (Container) which is a toplevel form. The code is run from the main form and IsMDIContainer is set to True. Yet it still opens as a new window.

Is there anyway to open this in my container. I have the following code below.

public void Open_Modal_Form(string formname)
        {

            Type type = Type.GetType("MyProject.Forms." + formname );
            object obj = Activator.CreateInstance(type);
            this.IsMdiContainer = true;
            (obj as Form).ShowDialog(this);

        }
Type type = Type.GetType("WindowsFormsApplication1.Form2");
    object obj = Activator.CreateInstance(type);
    (obj as Form).MdiParent = this; // Form1
    (obj as Form).Show();

Strange, that brings up nothing. My form is not showing when I try that line.

Also to bring up a Modal form it gives me an error too.

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();

        }

        public void Open_Modal_Form(string formname)
        {

            Type type = Type.GetType("myProject.Forms." + formname );
            object obj = Activator.CreateInstance(type);
            (obj as Form).MdiParent = this;
            (obj as Form).ShowDialog(this); // This gets an error: Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.

//Yet MainForm has this.IsMdiContainer = true;

        }

The error is telling you that your secondary form cannot have a parent. Remove the code saying .MdiParent = this before you make the call to .ShowDialog().

As for the first issue, I'm afraid I don't know what to tell you.

But What about if the form in other project ?
Your code return null
Type type = Type.GetType(formname); // This == null
My Fully class name is GL.Operations.frmAccounting

commented: very good -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.