【问题标题】:Check all items in a specified checked list box c#检查指定选中列表框中的所有项目c#
【发布时间】: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


【解决方案1】:

首先我将描述结构
Order = TableLayoutPanel
TableLayoutPanel 有 1 个 CheckAll ButtonCheckListBox
您希望当您点击 CheckAll Button 时,它会准确检查当前 TableLayoutPanel 中的所有项目。
所以试试这段代码

class XForm : Form {
    // create Dictionary to store Button and CheckListBox
    IDictionary<Button, CheckListBox> map = new Dictionary<Button, CheckListBox> ();

    // when you create new order (new TableLayoutPanel)
    // just add map Button and CheckListBox to map
    private void CreateOrder () {
        var panel = new Panel ();
        panel.Size = new System.Drawing.Size (360, 500);
        panel.BorderStyle = BorderStyle.FixedSingle;
        panel.Name = "panel";

        var table = new TableLayoutPanel ();

        var checklistBox = new CheckedListBox ();
        var button = new Button () { Text = "CheckAll" };

        table.Controls.Add (button, 1, 4);
        button.Name = "b1";
        button.Click += new EventHandler (CheckAll_Click);
        button.AutoSize = true;
        map[button] = checklistBox;
    }

    // and on event handle
    private void CheckAll_Click (object sender, EventArgs e) {
        var buttonClicked = (Button) sender;
        var c = map[buttonClicked];
        if (c == null) return;
        for (int i = 0; i < c.Items.Count; i++)
        {
            c.SetItemChecked(i, true);
        }
    }
}

删除订单时不要将其从地图中删除。
希望对你有帮助

【讨论】:

  • 关于事件句柄我将如何检查checkedlistbox中的所有项目?你能把剩下的代码提供给我吗..提前谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多