dynamicall load form

kcwallace

Centurion
Joined
May 27, 2004
Messages
175
Location
Austin, TX
I was wondering if there was a way to dynamically load a form with a function similar to the following:

Visual Basic:
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
 
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.
 
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.
 
Use a provider pattern, and send whatever id that defines a form to the provider and have the provider return the object.
 
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?
 
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.
 
Back
Top