kcwallace Posted June 15, 2006 Posted June 15, 2006 I was wondering if there was a way to dynamically load a form with a function similar to the following: function openform(formName as string) as form dim f as new form 'some code here to open what ever form is named "formName" return f end function Quote Go Beavs!!!
Administrators PlausiblyDamp Posted June 15, 2006 Administrators Posted June 15, 2006 The .CreateInstance method of the Assembly class is probably a good place to start looking. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted June 15, 2006 Leaders Posted June 15, 2006 What would you be using this for? Where is the string coming from? You may find a select case to be the easiest route. If you need to be a little more dynamic, the Assembly class is probably the solution. Things can get tricky if you aren't sure which assembly the type belongs to, so if you are using this for some sort of plug-in system or anything along those lines, a System.Type might be a little more approprite. And then, if you need to serialize type info, the Type.AssemblyQualifiedTypeName and Type.GetType functions come in handy. Quote [sIGPIC]e[/sIGPIC]
kcwallace Posted June 15, 2006 Author Posted June 15, 2006 I have a dynamically built form that adds a series of buttons. each button loads a specific form. Each button is defined by a database record. I currently am using a case statement to determine which form to load, but that has to be coded everytime something new is added. I would like the button to automatically open the form defined by the database record. Quote Go Beavs!!!
Diesel Posted June 19, 2006 Posted June 19, 2006 Use a provider pattern, and send whatever id that defines a form to the provider and have the provider return the object. Quote
kcwallace Posted June 19, 2006 Author Posted June 19, 2006 What is a pattern provider. Do you have a help file? Quote Go Beavs!!!
Diesel Posted June 19, 2006 Posted June 19, 2006 Forget what I said. Either way you do it...you have to have a way to relate the form to the id you give it in the database. If not a select case...a config file....an enumeration All will have to be updated each time a new form is created if you use activator.createinstance...what are you going to use to define the form? are you going to keep the actual class name in the database? Quote
Leaders snarfblam Posted June 20, 2006 Leaders Posted June 20, 2006 Activator.CreateInstance can take a System.Type, which can be created from an assembly-qualified type name. If you have an assembly-qualified type name in the DB, you can pull it and instantiate it without having to worry about which assembly it belongs to (assuming that the assembly is loaded). If the forms are all in the same assembly and you know which assembly they are in then make life easier and just use PD's suggestion. Quote [sIGPIC]e[/sIGPIC]
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.