简体   繁体   English

用户控件c#动态添加

[英]User Control c# Add Dynamically

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 当我将此控件添加到我的图片框时,myObject包含一个按钮等

picturebox.Controls.Add(myobject);

my picturebox's backgorund image is dissappeared. 我的画框的backgorund图像消失了。

Why? 为什么?

note: the button can be seen however. 注意:但是可以看到按钮。 I want picturebox to be seen also 我也希望看到图片框

Set transparent basckground color of your user control. 设置用户控件的透明basckground颜色。 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): 是的,正如@samjudson所述,不应以这种方式使用PictureBox:尝试将Panel与背景图像一起使用(方法将保持不变-使用透明颜色查看父控件):

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

The PictureBox control is not meant to be used as a container. PictureBox控件不能用作容器。 Try adding a parent Panel or similar and the adding the PictureBox and your custom control to the Panel control. 尝试添加父面板或类似面板,然后将PictureBox和自定义控件添加到面板控件中。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM