Home All Groups Group Topic Archive Search About

Converting a string to a Form object

Author
28 Sep 2006 5:07 PM
Kevin S Gallagher
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.Name & ".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!!!

Author
29 Sep 2006 11:40 AM
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.Name & ".frmChildOne"
Dim f As Form =
Reflection.Assembly.GetExecutingAssembly.CreateInstance(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


Show quoteHide quote
"Kevin S Gallagher" <kevin.s.gallag***@state.or.us> wrote in message
news:%23WOHGCy4GHA.5012@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.Name & ".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!!!
>