468,134 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,134 developers. It's quick & easy.

Converting a string to a Form object

I found this code (pretty sure it was from a MVP) for converting a string
variable to a form object which works fine within a form. Take the code and
place it into a code module and it fails on the second line in regards to
Me.GetType.... any idea how to get this to work outside of a form?

Dim strClass As String =
Reflection.Assembly.GetExecutingAssembly.GetName.N ame & ".frmChildOne"
Dim tyOfStringVariable As Type =
Me.GetType().Assembly.GetType(strClass)
Dim frmObject As Object = Activator.CreateInstance(tyOfStringVariable)
Dim f As Form = CType(frmObject, Form)
With f
.ShowDialog()
.Dispose()
End With
Thanks for any insight!!!
Sep 28 '06 #1
1 1645
PGC
Hi Kevin,

That's because "Me" refers to the instance of the form object the code was
originally placed in but a module is shared and doesn't have an instance. If
you're sure that you're only going to create a new standard windows form
object from the string provided then you could try;

Dim strClass as String =
Reflection.Assembly.GetExecutingAssembly.GetName.N ame & ".frmChildOne"
Dim f As Form =
Reflection.Assembly.GetExecutingAssembly.CreateIns tance(strClass)
With f
.ShowDialog()
.Dispose
End With

Note: The above will fail if the string (".frmChildOne") is not of type
windows.forms.form or the form doesn't exist. The module containing the
above code should stay with the same assembly as the forms you wish to
create from it.

Hope this helps.

PGC
"Kevin S Gallagher" <ke***************@state.or.uswrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>I found this code (pretty sure it was from a MVP) for converting a string
variable to a form object which works fine within a form. Take the code and
place it into a code module and it fails on the second line in regards to
Me.GetType.... any idea how to get this to work outside of a form?

Dim strClass As String =
Reflection.Assembly.GetExecutingAssembly.GetName.N ame & ".frmChildOne"
Dim tyOfStringVariable As Type =
Me.GetType().Assembly.GetType(strClass)
Dim frmObject As Object =
Activator.CreateInstance(tyOfStringVariable)
Dim f As Form = CType(frmObject, Form)
With f
.ShowDialog()
.Dispose()
End With
Thanks for any insight!!!

Sep 29 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by amitbadgi | last post: by
3 posts views Thread by excelleinc.com | last post: by
4 posts views Thread by gg9h0st | last post: by
5 posts views Thread by k.vanderstarren | last post: by
25 posts views Thread by Blasting Cap | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.