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

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

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....
2 Solutions
Check out this....

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 http://www.codeproject.com/aspnet/LoadingUSerControl.asp

JRossi1Author Commented:

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?
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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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