Opening forms based on form Type

Posted on 2006-05-25
Medium Priority
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 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 16761475
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

ID: 16762009
Works like a charm.  Thanks!

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

864 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