How to insert a check box inside a list box in C#?

前端 未结 2 561
盖世英雄少女心
盖世英雄少女心 2021-01-21 15:26

I want a code to insert a checkbox inside a listbox in c sharp. on selecting the checkbox all the items in listbox must get selected.

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 16:04

    You can use a CheckListBox to display a list with a check box next to each item.

    But to make a single checkbox that selects everything in a list, it must be outside the list box (above or below or beside it). Then you can use code like:

    public void SelectAllCheckBox_CheckedChanged(object s, EventArgs e) 
    {
        foreach (var item in ListBox1.Items) 
        {
            item.Selected = SelectAllCheckBox.Checked;
        }
    }
    

    There is no control that has a single check box inside a list: eg this is what you mean:

    +----------------------------------------+
    | [x] Select All                         |
    | Item one                               |
    | Item two                               |
    | Item three                             |
    | Item four                              |
    | Item five                              |
    +----------------------------------------+
    

    Instead you must use two controls: a checkbox and a separate list box:

    [x] Select All                         
    
    +----------------------------------------+
    | Item one                               |
    | Item two                               |
    | Item three                             |
    | Item four                              |
    | Item five                              |
    +----------------------------------------+
    

提交回复
热议问题