Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使控件在面板中可见?_C#_Winforms - Fatal编程技术网

C# 如何使控件在面板中可见?

C# 如何使控件在面板中可见?,c#,winforms,C#,Winforms,我想在WinForms中将控件添加到我的面板中 public Form1() { InitializeComponent(); PictureBox a = new PictureBox(); a.Left = 100; a.Top = 150; a.Width = 50; a.Height = 50; a.BackColor = Color.Red; Controls.Add(a)

我想在WinForms中将控件添加到我的面板中

public Form1()
{
       InitializeComponent();
       PictureBox a = new PictureBox();
       a.Left = 100;
       a.Top = 150;
       a.Width = 50;
       a.Height = 50; 
       a.BackColor = Color.Red;
       Controls.Add(a);    
}

没有面板,这段代码工作得很完美。但是面板会阻止PictureBox,我应该更改哪些属性

基本上,实现这一目标的选择很少:

1。将控件添加到
表单时,如下所示:

Controls.Add(panel);
Controls.Add(button1);
Controls.Add(button2);
Controls.Add(pictureBox);
它们将按如下顺序显示:
面板
位于底部,
按钮
位于中间,而
图片框
位于顶部

2。正如注释中指出的,添加控件后,可以使用
BringToFront()

pictureBox.BringToFront();
这将使
图片盒
位于其他内容之上

3。您可以通过编辑控件的Z索引(在WinForms中称为
ChildIndex
)来更改控件的顺序。您可以将其设置为:

Controls.SetChildIndex(pictureBox, __yourIndex__);
4.您可以使用以下功能将
图片盒添加到
面板中:

panel.Controls.Add(pictureBox);

使用BringToFront()。或者将图片框添加到面板。使用
面板显示代码
@HansPassant谢谢我一直在寻找类似的东西。太好了!谢谢你,先生。