Tuesday, April 6, 2010

How to programmatically check items in CheckedListBox

You can simply use CheckedItemBox.SetItemChecked(int index, bool value).

If you need to set the value based on the values from an enum with the [Flag] attribute you can do this:

cbDays.SetItemChecked(0, (curSchedule.DaysOfWeek & DaysOfWeek.Monday) == DaysOfWeek.Monday);
Where cbDays is a CheckedItemBox.
0 means we are looking at the first item in the CheckedItemBox. In this case it's "Monday".

The next part, curSchedule.DaysOfWeek & DaysOfWeek.Monday) == DaysOfWeek.Monday is doing a bitwise AND to see if Monday is a selected value.


References:
1. http://msdn.microsoft.com/en-us/library/t0zxd5z5%28vs.71%29.aspx

No comments:

Post a Comment

There was an error in this gadget