Wednesday, June 2, 2010

How to delete all checked items in CheckedListBox

Remove all checked items from a CheckedListBox
for (int i = checkListDays.Items.Count - 1; i >= 0; i--)
if (checkListDays.GetItemChecked(i))

Another solution is to save all the items in a List by iterating through CheckedListBox.CheckedItems, and then iterating through the resulting List and CheckedListBox.Items.Remove(object from List<>).

These solutions differ slightly in efficiency. The first solution iterates through the CheckedListBox only once, thanks to on-the-fly item removal, thus O(n). The second solution could iterate through the CheckedItems list, which could contain all items, and then iterate through the List<>, thus O(2n). But, O(2n) == O(n), because the 2 is dropped, thus the difference in efficiency is insignificant. I do like the first solution more however, because of its elegance.

Delete all checked items in CheckedListBox | debugging:

Tuesday, June 1, 2010

Read-only ComboBox in Windows Form

You are using a ComboBox as a drop down list and want to disallow the user from typing in the box.

Set the ComboBox's DropDownStyle to "DropDownList"

Make combobox readonly - C# / C Sharp answers