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....
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.