Results 1 to 3 of 3

Thread: [RESOLVED] How to open a form using a string?

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Resolved [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:
    1. Dim frmName As String
    2.         Dim objFRM As New Form
    3.  
    4.         Try
    5.             frmName = tmp(1)
    6.             objFRM = CreateObject(frmName)
    7.         Catch ex As Exception
    8.             modgen_ErrorLog(ex.Message.ToString)
    9.         End Try
    10.  
    11.         Try
    12.             objFRM.show
    13.         Catch ex As Exception
    14.             modgen_ErrorLog(ex.Message.ToString)
    15.         End Try
    With that code I get this error:
    "Cannot create ActiveX component"

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    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 ?
    thanks
    amrita

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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:
    1. Dim asm = System.Reflection.Assembly.GetExecutingAssembly
    2.  
    3. Dim myTypes As Type() = asm.GetTypes()
    4. Dim frm As Form
    5.  
    6. For Each t As Type In myTypes
    7.     If t.IsSubclassOf(GetType(System.Windows.Forms.Form)) AndAlso TextBox1.Text = t.Name Then
    8.         frm = CType(Activator.CreateInstance(t), Form)
    9.         frm.Show()
    10.     End If
    11. Next

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width