【发布时间】:2019-09-26 08:19:40
【问题描述】:
我创建了一个显示食品订单的小型厨房展示程序。因此,我动态创建了一个面板,其中包含一个表格布局面板,该面板包含一个选中的列表框和一个全选按钮。我的问题是...我在每个动态创建的表格布局面板中都有一个检查所有按钮,每次单击它时,它都会检查最后创建的 CheckedListBox 中的所有项目,而不是单击的项目。
这是我的代码:
p = new Panel();
p.Size = new System.Drawing.Size(360, 500);
p.BorderStyle = BorderStyle.FixedSingle;
p.Name = "panel";
tpanel = new TableLayoutPanel();
tpanel.Name = "tablepanel";
clb = new CheckedListBox();
tpanel.Controls.Add(b1 = new Button() { Text = "CheckAll" }, 1, 4);
b1.Name = "b1";
b1.Click += new EventHandler(CheckAll_Click);
b1.AutoSize = true;
private void CheckAll_Click(object sender, EventArgs e)
{
var buttonClicked = (Button)sender;
var c = GetAll(this, typeof(CheckedListBox));
for (int i = 0; i < c.Count(); i++)
{
\\any help
}
}
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type)).Concat(controls).Where(c =>
c.GetType() == type);
}
【问题讨论】:
-
它检查最后创建的checkedlistbox中的所有项目,而不是点击的项目我不明白这部分。你的问题是什么? GetAll() 方法返回空还是什么?这是 WinForm,对吧?
-
每次有新订单时,都会使用上述项目(tablelayooutpanel + checklistbox 和按钮)动态创建一个新面板,因此当我收到多个订单时,我想按特定顺序检查项目。例如,如果我有 3 个订单,则将创建 3 个面板。那么我将如何检查第二个订单中的所有项目。那是我的问题
-
好的,我明白了。请等待几分钟的答案:-)
-
是的,这是一个 Windows 窗体
标签: c# panel tablelayoutpanel checkedlistbox