Opening forms based on form Type

Posted on 2006-05-25
Last Modified: 2008-01-09
I'm currently trying to create a unified function for opening some forms, but there are some problems when trying to utilize the Types.  

For example, lets say I have 5 forms:  Form1, Form2, etc.

Calling the function, I would like to use:  OpenFunction(GetType(Form1))

A sample of what I've tried is:

    Public Function OpenForm(ByVal FormType As Type)
        Dim frm As New Form

        'Find if a form of that type is already open (found in the Forms Collection).  Returns a reference to the form.
        frm = Forms.FindForm(FormType)

        'If the form is already open, bring it to the front
        If Not frm Is Nothing Then
            'Otherwise, the form is not open, so create a new one
            frm = New ??? 'Here is the problem
        End If

        'Show the form
    End Function

Is there any way to create a New form, given the Type?  
Question by:ZeonFlash
    LVL 85

    Accepted Solution

    See the Activator.CreateInstance Method (Type) :

    VB.Net 2005 code:

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        End Sub

        Public Sub OpenForm(ByVal FormType As Type)
            Dim frm As Form
            For Each frm In Application.OpenForms
                If frm.GetType.Equals(FormType) Then
                    If frm.WindowState = FormWindowState.Minimized Then
                        frm.WindowState = FormWindowState.Normal
                    End If
                    Exit Sub
                End If

            frm = CType(Activator.CreateInstance(FormType), Form)
        End Sub

    End Class
    LVL 17

    Author Comment

    Works like a charm.  Thanks!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now