Create form from name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shayne H

    Create form from name

    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.Asse mbly.CreateInst ance(FormName, False)
    Return CType(obj,Form)
    End Function
    End Class

    --
    Thanks for any help,
    Shayne H


  • CJ Taylor

    #2
    Re: Create form from name

    I've done something just like this just the other day.

    I used Activator along with that and used GetExecutingAss embly() instead...

    I have the code at work unfortunatly =(
    "Shayne H" <shaynehATlycos SPAMGOTOHELLcoD OTuk> wrote in message
    news:OOFzm52aDH A.1128@tk2msftn gp13.phx.gbl...[color=blue]
    > I was using the following code to create a form from its class name. It[/color]
    was[color=blue]
    > 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.Asse mbly.CreateInst ance(FormName, False)
    > Return CType(obj,Form)
    > End Function
    > End Class
    >
    > --
    > Thanks for any help,
    > Shayne H
    >
    >[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Create form from name

      Hello,

      "Shayne H" <shaynehATlycos SPAMGOTOHELLcoD OTuk> schrieb:[color=blue]
      > I was using the following code to create a form from its class name. It[/color]
      was[color=blue]
      > 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>[/color]

      This works for me:

      \\\
      Dim objNewForm As Object = _
      Activator.Creat eInstance( _
      Type.GetType("M yApplication.Sa mpleForm") _
      )
      Dim frm As Form = DirectCast(objN ewForm, Form)
      frm.Show()
      ///

      HTH,
      Herfried K. Wagner
      --
      MVP · VB Classic, VB .NET
      Die Website von H. Wagner zu .NET, Visual Basic .NET und Classic Visual Basic.



      Comment

      • CJ Taylor

        #4
        Re: Create form from name

        That was a lot simplier than what I did...


        "Herfried K. Wagner [MVP]" <hirf.nosp@m.ac tivevb.de> wrote in message
        news:eYLP3S8aDH A.2072@TK2MSFTN GP10.phx.gbl...[color=blue]
        > Hello,
        >
        > "Shayne H" <shaynehATlycos SPAMGOTOHELLcoD OTuk> schrieb:[color=green]
        > > I was using the following code to create a form from its class name. It[/color]
        > was[color=green]
        > > working then at some point I must have broken it, and now have no idea[/color][/color]
        why[color=blue][color=green]
        > > it will not work. The CreateInstance method returns <Nothing>[/color]
        >
        > This works for me:
        >
        > \\\
        > Dim objNewForm As Object = _
        > Activator.Creat eInstance( _
        > Type.GetType("M yApplication.Sa mpleForm") _
        > )
        > Dim frm As Form = DirectCast(objN ewForm, Form)
        > frm.Show()
        > ///
        >
        > HTH,
        > Herfried K. Wagner
        > --
        > MVP · VB Classic, VB .NET
        > http://www.mvps.org/dotnet
        >
        >[/color]


        Comment

        Working...