Results 1 to 4 of 4

Thread: Working with form class collection <Solved!>

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    2

    Working with form class collection <Solved!>

    I am new to vb.net, previous just an asp guy.

    I would like to create a startup form that displays a list box of all available forms and then by pressing a button, displays the form selected in the list box. This just helps the process of reviewing the UI of each form.

    This is not as easy as it sounds because there is not a forms collection. I have a couple of questions/thoughts:

    1) Forms are just classes in vb.net so there must be a way to loop through the classes and find ones whose names start with frm. Not elegant, but might work.

    2) I have the following code behind a button that is related to a text box
    ---------------------------------------------------

    Dim frmName As String
    Dim T As Type

    'Get form Name, put project name in front
    frmName = "prj_New_Project." & Me.txbx_Form_Name.Text

    T = GetType(frmName)
    Dim F As Form = Activator.CreateInstance(T)

    F.Show()

    -------------------------------
    There is a build error that says 'Type frmName is not defined'.

    3) Does anyone know how to get this to work?

    Thanks
    Last edited by shrub; Aug 3rd, 2003 at 07:31 PM.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    If you need the name of forms in an assembly you can use System.Reflection.Assembly
    VB Code:
    1. Dim alltypes As Type()
    2. Dim frmtype As Type
    3. alltypes = System.Reflection.Assembly.GetExecutingAssembly.GetTypes
    4. For Each frmtype In alltypes
    5.    If frmtype.IsSubclassOf(GetType(Form)) Then
    6.       Messagebox.Show(frmtype.FullName) ' FullName gives you the form name with its namespace
    7. ' , required to create an instnace, Name just gives you the type name.
    8.    End If
    9. Next
    Last edited by Lunatic3; Aug 3rd, 2003 at 03:59 PM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    To open a form with its name you may use this code:
    VB Code:
    1. Dim frm As Form = Activator.CreateInstance(Type.GetType("Namespace.Formname"))
    2. frm.Show
    NameSpace.FormName is the Full name of form type, that you can get it from the previous example.
    Last edited by Lunatic3; Aug 4th, 2003 at 12:23 AM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    2

    Thanks!

    That worked brilliantly! Thanks for your help.

    For the record, here is what I did:

    In the load event of the display form I put:

    Dim alltypes As Type()
    Dim frmtype As Type
    alltypes = System.Reflection.Assembly.GetExecutingAssembly.GetTypes

    For Each frmtype In alltypes
    If frmtype.IsSubclassOf(GetType(Form)) Then
    'MessageBox.Show(frmtype.FullName) ' FullName gives you the form name with its namespace (Use MessageBox for testing)
    ' Note: FullName is required to create an instance Name just gives you the type name.
    ' On the form is a listbox named ct_lst_Form_List, and adding the fullname of the form to that list.
    ctl_lst_Form_List.Items.Add(frmtype.FullName)
    End If
    Next

    On the form is a button with the following in the click event:

    Dim frmFullName As String
    frmFullName = Me.ctl_lst_Form_List.SelectedItem
    Dim frm As Form = Activator.CreateInstance(Type.GetType(frmFullName))
    frm.Show()


    Select the form name in the list box and press the button, the form opens. Yipee!

    Thanks again!

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