Create a new instant of a form from a parameter passed to a sub

Posted on 2009-02-18
Last Modified: 2012-05-06
Hi Experts,
I have some code that opens a form inside a panel control. It worked fine to open one form. I would like to reuse the code to open a different form in the panel. I tried to pass the form as a parameter with no luck.
Thank You
Public Class frmTakeOff

Dim SubFrmTakeOffSubWalls As form

 Private Sub OpenTakeOffSubForm(ByVal SubFormName As Form, ByVal Edited As Boolean)

SubFrmTakeOffSubWalls = CType(Activator.CreateInstance(SubFormName.GetType()), Form)

        With SubFrmTakeOffSubWalls

            .TopLevel = False

            .Dock = DockStyle.Fill

            .Anchor = AnchorStyles.Bottom

            .WindowState = FormWindowState.Maximized

            If Edited = True Then

                .SubWallEditMode = True

                .CurrentTakeOffItemId = Me.TakeOffGridView.GetFocusedRowCellValue("TakeOffItemId")

            End If


            AddHandler .UpdateTakeOffGrid, AddressOf FillTakeOffGrid



        End With

    End Sub

Open in new window

Question by:DjJohnny
    LVL 9

    Accepted Solution

    It might have been easier to have 2 userControls that are built to your specs, then just, .remove(the old one) then .add(the new one). It also sounds like you are changing a value that causes your program to remove the current object and then add the same object only modified. Forms use the .show and controls use .add and you have both which may be the problem. What is the purpose for the new form?
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    You have declared your variable (being used in the "with" statement) as a generic Form:

        Dim SubFrmTakeOffSubWalls As form

    Thus it has no idea what these properties are:


    Or what this event is:


    If you have multiple form types that ALL have these Properties/Events, then make an INTERFACE that all of these Forms Implement and declare your "With" variable as that type instead:

        Dim SubFrmTakeOffSubWalls As YourInterfaceHere

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 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

    16 Experts available now in Live!

    Get 1:1 Help Now