Create form from name

  • Thread starter Thread starter Shayne H
  • Start date Start date
S

Shayne H

I was using the following code to create a form from its class name. It was
working then at some point I must have broken it, and now have no idea why
it will not work. The CreateInstance method returns <Nothing>

Class MyClass
Function OpenForm(ByVal FormName As String) As Form
Dim obj As Object
obj = Me.GetType.Assembly.CreateInstance(FormName, False)
Return CType(obj,Form)
End Function
End Class
 
I've done something just like this just the other day.

I used Activator along with that and used GetExecutingAssembly() instead...

I have the code at work unfortunatly =(
 
Hello,

Shayne H said:
I was using the following code to create a form from its class name. It was
working then at some point I must have broken it, and now have no idea why
it will not work. The CreateInstance method returns <Nothing>

This works for me:

\\\
Dim objNewForm As Object = _
Activator.CreateInstance( _
Type.GetType("MyApplication.SampleForm") _
)
Dim frm As Form = DirectCast(objNewForm, Form)
frm.Show()
///

HTH,
Herfried K. Wagner
 
That was a lot simplier than what I did...


Herfried K. Wagner said:
Hello,



This works for me:

\\\
Dim objNewForm As Object = _
Activator.CreateInstance( _
Type.GetType("MyApplication.SampleForm") _
)
Dim frm As Form = DirectCast(objNewForm, Form)
frm.Show()
///

HTH,
Herfried K. Wagner
 
Back
Top