Results 1 to 3 of 3

Thread: Opening Forms At Run-Time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    London
    Posts
    26

    Opening Forms At Run-Time

    I have a number of forms in my project that are opened by selecting a menuitem, but I want to give the user the flexibility to decide what menuitem opens what form. In earlier versions of VB, I could refer to the forms collection to achieve this. But in VB.Net it seems that the name of the form must be known at design time e.g dim f as new frmMain. Does anyone know how I can supply frmMain at run-time.

    Thanks

    erimus
    erimus

  2. #2
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    To allow users to decide what menuitems open what form you can dynamically add the click handlers for each menuitem at runtime.
    i.e. have a procedure for the opening of each form depending on a parameter and then add a handler for the menuitem:-

    addhandler menuitem1.click, addressof Openform(1)
    addhandler menuitem2.click, addressof Openform(2)

    public sub Openform(number as short)
    if number = 1 then
    dim f as new form1
    f.show
    else
    dim f as new form2
    end if
    end sub

  3. #3
    Member
    Join Date
    May 2001
    Location
    Adelaide, Australia
    Posts
    51
    Is this closer to what you're looking for?

    VB Code:
    1. Public Function LoadForm(ByVal myFormName As String) As Form
    2.     Dim myType As Type
    3.     Dim myForm As Form
    4.  
    5.  
    6.     myType = System.Type.GetType(myFormName, True, True)
    7.  
    8.     myForm = Activator.CreateInstance(myType)
    9.  
    10.     Return myForm
    11. End Function

    Of course, you'll probably want some exception handling in there.

    Not sure, but you may need to change:
    VB Code:
    1. myType = System.Type.GetType(myFormName, True, True)
    to:
    VB Code:
    1. myType = System.Type.GetType("ProjectName." & myFormName, True, True)

    (Obviously, replacing "ProjectName" with your project )
    Matthew Draper
    [email protected]

    "Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard

    "I like long walks, especially when they are taken by people who annoy me." - Noel Coward

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