Runtime form Object Reference
-
I want to display a form, which(name of form) is stored into a table.After firing query to that table I am getting appropriate value (e.g "frmSTART") into a string variable(myfrm = "frmSTART"), now how can I treat a string variable value as an object? So that I can write: myfrm nSTART = new myfrm (); nSTART.Show();
Hallo
-
I want to display a form, which(name of form) is stored into a table.After firing query to that table I am getting appropriate value (e.g "frmSTART") into a string variable(myfrm = "frmSTART"), now how can I treat a string variable value as an object? So that I can write: myfrm nSTART = new myfrm (); nSTART.Show();
Hallo
Hello, What you asked for is not possible! .) If you know all the possible class names during design time, you simple could make a switch case over the returned string.
string myform = actual_returned_string;
switch(myform)
{
case "test1":
{
MyTest1 t1 = new MyTest1();
break;
}case "test2": { MyTest2 t1 = new MyTest2(); break; } default: break;
}
.) If you have to make it more dynamic, then you have to look into reflection. Therefore you should know the namespace of youre formclasses. You could use a method like this, which returns you a Form to Show.
using System.Reflection;
public System.Windows.Forms.Form GetFormByName(string _name)
{
Type t = Type.GetType(_name);
if (t != null)
{
ConstructorInfo c = t.GetConstructor(Type.EmptyTypes);
Object o = c.Invoke(new Object[0]);
return o as System.Windows.Forms.Form;
}
else
{
return null;
}
}And you can call it like that:
string namespace = //???
string myform = namespace+actual_returned_string;System.Windows.Forms.Form actForm = GetFormByName(myform);
Hope it works for you! All the best, Martin
-
Hello, What you asked for is not possible! .) If you know all the possible class names during design time, you simple could make a switch case over the returned string.
string myform = actual_returned_string;
switch(myform)
{
case "test1":
{
MyTest1 t1 = new MyTest1();
break;
}case "test2": { MyTest2 t1 = new MyTest2(); break; } default: break;
}
.) If you have to make it more dynamic, then you have to look into reflection. Therefore you should know the namespace of youre formclasses. You could use a method like this, which returns you a Form to Show.
using System.Reflection;
public System.Windows.Forms.Form GetFormByName(string _name)
{
Type t = Type.GetType(_name);
if (t != null)
{
ConstructorInfo c = t.GetConstructor(Type.EmptyTypes);
Object o = c.Invoke(new Object[0]);
return o as System.Windows.Forms.Form;
}
else
{
return null;
}
}And you can call it like that:
string namespace = //???
string myform = namespace+actual_returned_string;System.Windows.Forms.Form actForm = GetFormByName(myform);
Hope it works for you! All the best, Martin
I don't know why you got voted a 2 for that - I thought the reflection idea was sound.
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
I don't know why you got voted a 2 for that - I thought the reflection idea was sound.
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
Hello,
Colin Angus Mackay wrote:
I don't know why you got voted a 2 for that - I thought the reflection idea was sound.
I have also no idea! It would also be interesting for me to know way, cause I'm using reflection in my application. So thank's for youre statement! All the best, Martin
-
I don't know why you got voted a 2 for that - I thought the reflection idea was sound.
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
ednrgc wrote:
Probably because the answer was above the person's comprehension.
If someone gave me an answer that I didn't understand then I'd follow it up with a request for clarification. Oh, Well.... :sigh:
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos