User Control c# Add Dynamically

17,827

Solution 1

Set transparent basckground color of your user control. This will make picturebox visible:

UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
picturebox.Controls.Add(myobject);

BTW I believe you have different name of user control. And yes, as @samjudson stated, PictureBox should not be used this way: try to use Panel with background image instead (approach will stay same - use transparent color to see parent control):

panel.BackgroundImage = // your image
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
panel.Controls.Add(myobject);

Solution 2

Try this:

UserControl myobject = new UserControl();
Button but = new Button();
but.BackColor = Color.Gray
pic.BackColor = Color.Green;
myobject.Controls.Add(but);
pic.Visible = true;
pic.Controls.Add(myobject);

Solution 3

The PictureBox control is not meant to be used as a container. Try adding a parent Panel or similar and the adding the PictureBox and your custom control to the Panel control.

Share:
17,827
Birol Capa
Author by

Birol Capa

Updated on June 04, 2022

Comments

  • Birol Capa
    Birol Capa almost 2 years

    I create a user control dynamically in my code

    UserControl myobject = new UserControl();
    

    myObject contains a button etc. when I add this control to my picturebox

    picturebox.Controls.Add(myobject);
    

    my picturebox's backgorund image is dissappeared.

    Why?

    note: the button can be seen however. I want picturebox to be seen also