Create form based on string?

M

Mike

Is it possible to create and display a form based on a string variable that
will be set at run-time?

In other words, instead of declaring a variable as a user-defined form
class:

Dim MyForm as New frmMyForm

is it possible to do something like

Dim sClassName as String
Dim MyForm as Form

sClassName = "frmMyForm"
MyForm = New sClassName

?
 
C

CJ Taylor

Public Sub LoadForm(ByVal formType As Type, ByVal fParent As Form, Optional
ByVal Singleton As Boolean = True, Optional ByVal Modal As Boolean = False)

Dim cThread As System.Threading.Thread

Dim cTPool As System.Threading.ThreadPool

Dim tForm As System.Windows.Forms.Form

Try

tForm = FormAlreadyExists(fParent, formType)

If (tForm Is Nothing) Then

tForm = Activator.CreateInstance(formType)

If (fParent.IsMdiContainer) Then

tForm.MdiParent = fParent

End If

Else

If (Singleton) Then

tForm.Focus()

Else

tForm = Activator.CreateInstance(formType)

If (fParent.IsMdiContainer) Then

tForm.MdiParent = fParent

End If

End If

End If

If (Modal) Then

tForm.ShowDialog(fParent)

Else

tForm.Show()

End If

Catch ex As Exception

End Try

End Sub
 
C

CJ Taylor

One more thing... the code I sent you so to use a type..

you can modify that with Type.GetType("myFormType")

-CJ
 

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