Cywizz Posted February 19, 2003 Posted February 19, 2003 Hi all I've got a form name (string) that was read from a database field. I want to take this string and create a form instance and invoke the show/showdialog method on it. Can anyone help me with this? Thanks in advance :) Quote Howzit??
*Experts* Volte Posted February 19, 2003 *Experts* Posted February 19, 2003 I don't understand. Do you mean you want to create a form that is named whatever the string is? I don't think that's possible, considering forms don't have Name properties anymore (their names are their object names; there is no seperate string property like in VB6). You weren't exactly clear about how the string ties in with the form instance. :confused: Quote
sethindeed Posted February 20, 2003 Posted February 20, 2003 I am able to assign a Name property to my forms ( with custom classes ). Quote What I don't know keeps me excited...
Cywizz Posted February 20, 2003 Author Posted February 20, 2003 (edited) Let me clarify: I have a project that contains n amount of Forms. I store the class names (form names e.g. Form1 - nothing to do with the Name property) in a database. From a main form I would like to dynamicaly create a instance of one of these forms, using it's class name, and fire the Show/ShowDialog methods. Edited February 20, 2003 by Cywizz Quote Howzit??
Cywizz Posted February 20, 2003 Author Posted February 20, 2003 No worries, got the solution... if interested: //In form1. if you want to create instance of form2 (the same assembly) Type theObjectType = Type.GetType("YourNamespace.Form2"); Object theObject = Activator.CreateInstance(theObjectType); //Get the method and invoke it System.Reflection.MethodInfo m = theObjectType.GetMethod("Show"); m.Invoke(theObject,null); Quote Howzit??
*Gurus* divil Posted February 20, 2003 *Gurus* Posted February 20, 2003 Another, possibly better way: Form myForm = (Form)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("YourNamespace.Form2"); myForm.Show(); This question has been asked on the forum before. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Cywizz Posted February 20, 2003 Author Posted February 20, 2003 Thanks divil. This would be easier, if I knew the type and method - I also hold the object method in the database (Show or ShowDialog), so I'll need to call a method dynamically. This project might progress to other data types, not just forms as well Thanks again! Quote Howzit??
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.