We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


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

DjJohnny asked
Medium Priority
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

Watch Question

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?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.