[RESOLVED] How to open a form using a string?
I have a textbox on my form. I want the form which name is typed into the textbox (if exists) to open. How do I get this to happen?
vb.net Code:
Dim frmName As String
Dim objFRM As New Form
Try
frmName = tmp(1)
objFRM = CreateObject(frmName)
Catch ex As Exception
modgen_ErrorLog(ex.Message.ToString)
End Try
Try
objFRM.show
Catch ex As Exception
modgen_ErrorLog(ex.Message.ToString)
End Try
With that code I get this error:
"Cannot create ActiveX component"
Re: How to open a form using a string?
Is your textbox in a MDI parent form and you want to open the child form ?
Re: How to open a form using a string?
Because .Net Forms are NOT ActiveX components.
And what is this anyway? Why you need so cumbersome method of opening forms?
You can probably have a string collection of your forms names and open an appropriate form instance based on user input but I still fail to see any use for that? It's impractical for a real user interface since users tend to make mistakes and if it's simply to see whether it would work then the answer is no, it wouldn't.
P.S. Well, there's still a way to do it:
vb Code:
Dim asm = System.Reflection.Assembly.GetExecutingAssembly
Dim myTypes As Type() = asm.GetTypes()
Dim frm As Form
For Each t As Type In myTypes
If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) AndAlso TextBox1.Text = t.Name Then
frm = CType(Activator.CreateInstance(t), Form)
frm.Show()
End If
Next