How to call a form if you know the form name only as a string variable >
-
Hi I have a small problem here which I am trying to solve . I have many Windows Forms in my application which I want to call and show each one based on its name , the name is stored in a database table as a string variable , this table is changeable by the user . I repeat the only thing I know about the Form is its name as a string variable . assume no parameters are to be passed to the Form > any Ideas Thanks We will Either find a way , or make one .
-
Hi I have a small problem here which I am trying to solve . I have many Windows Forms in my application which I want to call and show each one based on its name , the name is stored in a database table as a string variable , this table is changeable by the user . I repeat the only thing I know about the Form is its name as a string variable . assume no parameters are to be passed to the Form > any Ideas Thanks We will Either find a way , or make one .
Is the form you're looking for already opened? If so, you could call
Application.OpenForms
and find the opened form based on the name. If your form isn't opened and you need to instantiate and display one based on the name, you can use Reflection. In particular, something likeGetType.Assembly.GetType("TheNameGoesHere")
Life, family, faith: Give me a visit. From my latest post: "And you think, 'To keep my anti-Judaic theology alive I must reinterpret this verse too as being a blessing for Christians and not for Jews. I know it strains all manner of principles of interpretation. I don’t read the newspaper this sloppily, but, man, I have a theology to defend.'" Judah Himango
-
Hi I have a small problem here which I am trying to solve . I have many Windows Forms in my application which I want to call and show each one based on its name , the name is stored in a database table as a string variable , this table is changeable by the user . I repeat the only thing I know about the Form is its name as a string variable . assume no parameters are to be passed to the Form > any Ideas Thanks We will Either find a way , or make one .
-
Is the form you're looking for already opened? If so, you could call
Application.OpenForms
and find the opened form based on the name. If your form isn't opened and you need to instantiate and display one based on the name, you can use Reflection. In particular, something likeGetType.Assembly.GetType("TheNameGoesHere")
Life, family, faith: Give me a visit. From my latest post: "And you think, 'To keep my anti-Judaic theology alive I must reinterpret this verse too as being a blessing for Christians and not for Jews. I know it strains all manner of principles of interpretation. I don’t read the newspaper this sloppily, but, man, I have a theology to defend.'" Judah Himango
can you be more clear about the second case , Create an instance of a form by the form name as string only ? thanks
-
can you be more clear about the second case , Create an instance of a form by the form name as string only ? thanks
I pointed you in the right direction already: Type theFormWeAreLookingFor = this.GetType().Assembly.GetType("TheNameGoesHere") From there, you've got a Type. Look for the GetConstructor method, and call invoke on that. Voila! You have yourself a form. Read up on Reflection if you need more -- Gareth linked to the MSDN article in his reply to you.