我在 WinForms 中有一个面板,它在方法调用期间在运行时加载面板。
我的代码如下:
//Adding a child panel
Panel p = new Panel();
//Adding controls to panel
Label lbl5 = new Label();
lbl5.Location = new Point(105, 3);
lbl5.Text = note.noteName;
Label lbl6 = new Label();
lbl6.Location = new Point(105, 43);
lbl6.Text = note.noteName;
p.Controls.Add(lbl5);
p.Controls.Add(lbl6);
//Adding child panel to main panel
Panel1.Controls.Add(p);
这样,无论何时调用该方法,都会将一个新的子面板添加到主面板。
我可以单击显示在主面板中的特定面板吗?
我想获取所选面板中存在的控件的值并将其显示在某处。
我将不胜感激。
为您的面板命名....
var pPanel = new Panel();
pPanel.Name = "pPanel";
// or write it this way....using object initializer
var pPanel = new Panel
{
Name = "pPanel"
};
然后循环遍历您主面板中的控件以找到您正在寻找的控件...
foreach(Control ctrl in mainPanel)
{
if (ctrl.Name.Contains("pPanel")) .... then do something etc...;
}
您也可以用同样的方法在您的面板中搜索其他控件...
我是一名优秀的程序员,十分优秀!