Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
String variable has form name. (same project)

How to Open / Close this form
Posted
Comments
[no name] 24-Aug-13 6:55am    
If you are asking what I think you are asking from your 2 vague incomplete sentences, use reflection if you really want to do this.

1 solution

It's a fairly poor idea - it normally indicates that your design is flawed for a simple task - but you can do it with reflection as ThePhantomUpovoter has said:
C#
private void MyButton_Click(object sender, EventArgs e)
    {
    Type t = Type.GetType("MyNamespaceName.MyFormClassName");
    Form f = (Form) Activator.CreateInstance(t);
    f.Show();
    }
To close it, you can't really do it directly - you need to store the instance and close that to ensure that you are closing the correct form - you can get the list of open forms with Application.OpenForms, but if you open more than one of the same class, it is likely that they will be pretty much indistinguishable to you.

[edit]
Pah! VB...
VB
Dim t As Type = Type.[GetType]("MyNamespaceName.MyFormClassName")
Dim f As Form = DirectCast(Activator.CreateInstance(t), Form)
f.Show()

[/edit]
 
Share this answer
 
v2
Comments
pradeep20912 24-Aug-13 13:13pm    
Thanks for your answer,
In my project, one form (like password check) showing with several another forms,
I try to "close" and "Show" calling form with this,
OriginalGriff 25-Aug-13 3:32am    
If you mean that you have a form which displays a login form and hides, then when the user has logged in correctly the login form disappears and the main form shows again, or a new form is displayed instead, then this is very much the wrong approach to take!
There are much, much simpler and better solutions if that is what you are trying to do.

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