locked
How to add a User Control to a Windows Form dynamically? RRS feed

  • Question

  • I have a Windows form (base form) and 3 different user controls. 

    such that,

    1. Base Form
    2a - UserControl1
    2b - UserControl2
    2c - UserControl3

    I put a Panel Control on the Base Form, and I have 3 different buttons.  

    e.g.
    btnShowUserControl1
    btnShowUserControl2
    btnShowUserControl3

    I would like to ask know, for example, how I can show (or call) UserControl2 if I click on btnShowUserControl2 button.

    If possible, would you please show me some coding so that I can try it immediately, thanks ~

     

    Friday, August 27, 2010 3:39 PM

Answers

  • Something like this:

    UserControl1 uc1 = new UserControl1();
    myForm.myPanel.Controls.add(uc1);

    You may also want to set some properties:

    uc1.Top = ...
    uc1.Left = ...

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Patriek van Dorp Friday, August 27, 2010 3:48 PM
    • Marked as answer by iHandler Friday, August 27, 2010 4:09 PM
    Friday, August 27, 2010 3:43 PM

All replies

  • Something like this:

    UserControl1 uc1 = new UserControl1();
    myForm.myPanel.Controls.add(uc1);

    You may also want to set some properties:

    uc1.Top = ...
    uc1.Left = ...

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed as answer by Patriek van Dorp Friday, August 27, 2010 3:48 PM
    • Marked as answer by iHandler Friday, August 27, 2010 4:09 PM
    Friday, August 27, 2010 3:43 PM
  • oh wow, thanks DeborahK ~ 

    btw, can I think of UserControl as a "part" of a windows form? 

    I mean, I know how to program a form and add control on it and do Database connection, handle different events.  But would this be similar to a UserControl in this situation?  (I plan to "switch" some of my forms to UserControl and then put all those UserControls on a Base Form)  My end-users would be happy to see the new UI as they can see all different forms into one "combine" form (thanks for Microsoft Base Form and UserControls) 

    Can I think of this way?  Please advice :-)

    Friday, August 27, 2010 4:09 PM
  • Think of a user control as just another control on your form. Just like you could think of the DataGridView control as a set of header labels and textboxes in rows and columns, you can think of the set of controls in your user control as "just another control".

    For example, if you wanted to dynamically add a simple Textbox control to your form, you would use the exact same code that I provided to you, except using TextBox instead of UserControl1.

    A user control is just another control.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Friday, August 27, 2010 4:35 PM
  • yes, I totally understand it.  ~ thanks a lot ~
    Saturday, August 28, 2010 2:00 AM