gpt4 book ai didi

c# - 在 WinForms 中动态获取面板中加载的控件详细信息

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:00 26 4
gpt4 key购买 nike

我在 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...;
}

您也可以用同样的方法在您的面板中搜索其他控件...

关于c# - 在 WinForms 中动态获取面板中加载的控件详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33497033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com