简体   繁体   中英

Check all items in a specified checked list box c#

I have created a small kitchen display program that display food orders. So I created dynamically a panel that contains a table layout panel that contains a checked list box and a check all button. My problem is... I have a check all button in each table layout panel created dynamically and every time I click it, it checks all items in the last created CheckedListBox not the clicked one.

This is my code:

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);
}

First I will describe the struct
Order = TableLayoutPanel
TableLayoutPanel has 1 CheckAll Button and CheckListBox
And you want when you click to CheckAll Button it will checks exactly all items in current TableLayoutPanel .
So try this code

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);
        }
    }
}

And dont for get remove it from map when remove the order.
Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM