VB.NET - Adding Custom User Control to a form at runtime

Posted on 2006-04-14
Last Modified: 2008-01-09
I have a main form that contains a ListView of user control names, i.e., Mortgage, Note, Arm, Application, etc....  These names correspond to a custom user control that I created.

I have another form called frmWorkspace.  Depending on what 'module' the user chooses from the ListView, I want to load/add the corresponding custom user control to frmWorkspace.  Therefore, if the user selects 'Mortgage' from the list, any previously added control will be removed, and the 'Mortgage' custom control will be added to frmWorkspace.

I would imagine that this should occur on the 'SelectedIndexChanged' event of the ListView.  I have tried many version of the following code to no avail:

Dim ctlMortgage As New Mortgage

I have searched EE but I could not find examples of dynamically adding/removing USER controls at runtime.

Any help would be greatly appreciated....
Question by:JRossi1
    LVL 10

    Expert Comment

    Check out this....

    LVL 10

    Expert Comment

    Adding Directive on a Webform

    First, you need to add a directive on a webform telling the webform that you are going to dynamically load the usercontrol. So in the webform, add the following directive:

    <%@ Reference Control = "WebUserControl1.ascx" %>

    Once this directive has place, you need to load the actual control in the webform. You can load the control anywhere in the form. A good place will be the Page_Init event. In the example, I have loaded the control in the Page_Load event.

        WebUserControl1 uc =
          (WebUserControl1) Page.LoadControl("WebUserControl1.ascx");

    Here in the code, WebUserControl1 is the name of the UserControl class through which we have created an instance namely "uc". The Page.LoadControl loads the control on the page. And finally the place holder control adds the control to its collection and displays it on the webform.

    You can always use drag and drop features of the usercontrol to include it on the page. But sometimes it's important to load when some event occurs in the application, and for that you can create the controls dynamically.

    ...this is from

    LVL 6

    Author Comment


    Sorry, I wasn't clear.  This is a Windows based app, NOT web.  Do you have any information about adding a user control to a windows form?
    LVL 14

    Assisted Solution

    The sample code that you have posted in your question should work perfectly!

    Maybe you need to modify some visual properties of the user control that are set during the initialization of the control, such as Visible, Left, Top, Width and Height.


      Nayer Naguib
    LVL 2

    Accepted Solution

    Add all the custom controls on the form at design time but make them not visible,
    when user wants to see one page just make that page visible.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    zeroFront challenge 7 58
    zeroMAx challenge 20 66
    hasOne  challenge 59 64
     shows up in Outlook, not OWA or on phone 3 45
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now