How to insert a check box inside a list box in C#?
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.
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 |
+----------------------------------------+
Maybe you could extend the mentioned CheckedListBox, and handle a few Events so that only the first CheckBox is visible (maybe some kind of formatting event would be good for that).
And don't forget to use the onCheckedChangeEvent, so that you (de-)select all elements on change of the checkbox-value.
精彩评论