• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

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

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

1 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?
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now