Generate a form by using form name as string
-
Hi all, I have a name of a windows form in string type. I would like to generate it in runtime. How can I succeed this? ex: //my form class is MainPage let's say. //I have : string nameOfPage = "MainPage"; //I am searching a way of generating a form of type MainPage by just using variable nameOfPage Thanks a lot!
-
Hi all, I have a name of a windows form in string type. I would like to generate it in runtime. How can I succeed this? ex: //my form class is MainPage let's say. //I have : string nameOfPage = "MainPage"; //I am searching a way of generating a form of type MainPage by just using variable nameOfPage Thanks a lot!
Create an object of the form and set the name of the object as MainPage
Form newForm = new Form();
newForm.Name = "frmMainPage";
newForm.Size = new Size(200, 200);
newForm.Location = new Point(10, 10);
newForm.Show();
Oops I read your question wrong:sigh:. You must be using what Colin saidLast modified: 11mins after originally posted --
-
Hi all, I have a name of a windows form in string type. I would like to generate it in runtime. How can I succeed this? ex: //my form class is MainPage let's say. //I have : string nameOfPage = "MainPage"; //I am searching a way of generating a form of type MainPage by just using variable nameOfPage Thanks a lot!
A form is just a class. You can use reflection to instantiate an object of a class if you know the name of the class. See the MSDN documentation in the
System.Reflection
namespace.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
Create an object of the form and set the name of the object as MainPage
Form newForm = new Form();
newForm.Name = "frmMainPage";
newForm.Size = new Size(200, 200);
newForm.Location = new Point(10, 10);
newForm.Show();
Oops I read your question wrong:sigh:. You must be using what Colin saidLast modified: 11mins after originally posted --
IMHO this is not OP asks.:confused: Perhaps he wants to dynamically create an instance of a class whose type is given by the
"frmMainPage"
string. Do you agree? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
IMHO this is not OP asks.:confused: Perhaps he wants to dynamically create an instance of a class whose type is given by the
"frmMainPage"
string. Do you agree? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
CPallini wrote:
Perhaps he wants to dynamically create an instance of a class whose type is given by the "frmMainPage" string. Do you agree?
Yeah, realized I read his question wrong