Creating dynamic forms from a varible name

S

salportaro

Hello:

Here is the situation. VB.NET

I have several forms and I have a XML file that can start with a
particular for for debugging/layout reasons. This is a mdi project.
Normaly you would load a form as follows:

Dim frmT As frmTrans
frmT = New frmTrans
frmT.formDate = Now.Date
frmT.MdiParent = Me
frmT.Show()

Thus you could have a menu option to kick off a child form. What I
need is a way to have the form class as a varible name. Such as:

dim f as string = "frmTrans"
Dim frmT As f
frmT = New frmTrans
frmT.formDate = Now.Date
frmT.MdiParent = Me
frmT.Show()

Is there a way of doing this?

Thanks,

Sal

sfp04212005
 
S

Simon Verona

I think you need to use reflection for this..

I do something similar, using an XML based menu structure with each menu
loading a dll and calling a function... The function may in turn load a
form.

This is done using the following function which is called such as follows:

obj=loadmebyname("myclassfilename.dll","myclass",nothing)
obj.functionname


Hope this helps.. function follows:
Regards
Simon
Public Shared Function LoadMeByName(ByVal vstrAssemblyName As String, ByVal
vstrClassName As String, ByVal vArgs() As Object) As Object

'************************************************************************************

'dynamically load object from assembly for late binding purpose

'
*************************************************************************************

Dim objAssembly As Reflection.Assembly

Dim objtemp As Object

Dim Params() As Object

'MsgBox(Application.StartupPath)

Try

If Dir(Application.StartupPath & "\" & vstrAssemblyName).Equals("") Then

Err.Raise(-1, "LoadMeByName", "assembly could not found, wrong assembly name
given ")

Exit Function

End If

objAssembly = objAssembly.LoadFrom(Application.StartupPath & "\" &
vstrAssemblyName)

objtemp = objAssembly.CreateInstance(vstrClassName, True,
Reflection.BindingFlags.CreateInstance, Nothing, vArgs, Nothing, Nothing)

If objtemp Is Nothing Then

Err.Raise(-1, "LoadMeByName", "assembly could not found, wrong assembly name
given ")

Exit Function

End If

' LoadMeByName = objtemp

Return objtemp

Catch

End Try

End Function
 
G

Guest

Try using reflection by importing the System.Reflection namespace. You can
do something like this:

Dim currentAsm As [Assembly] 'Instance of current executing assembly
Dim asmTypes() As Type 'An array of all type of the executing
assembly
Dim typeNamespace As String 'the namespace of a given type
Dim constructorArguments() As Object 'any parameters you pass for the New()
Dim typeToCreate As String = "frmTrans" 'The name of the type you want
Dim instanceOfType As Object 'The actual instance of the type.

'Create instance of executing assembly
currentAsm = [Assembly].GetExecutingAssembly()

'Get the types from the assembly
asmTypes = currentAsm.GetTypes()

'Iterate through the types and find the one you want
For Each objType As Type In asmTypes
'Get the namespace of the current type
typeNamespace = objType.Namespace & "."

'If we find a match, then create an instance of that type
If Type.GetType(typeNamespace & typeToCreate, False, True) Is objType Then
'Create your arguments if necessary
args = new object() {"argument1"}

'Create instance of type
instanceOfType = objType.InvokeMember(Nothing, _
BindingFlags.Public Or BindingFlags.NonPublic Or _
BindingFlags.Instance Or BindingFlags.CreateInstance _
, Nothing, Nothing, args)
End If
Next

Doing a search on Reflection and pull up other examples. Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top