Hi,
I am using UltraComboEditor control with the following properties:-
DropDownStype = DropDownList
CheckedListSettings:
CheckBoxStyle= CheckBox
EditorValueSource = CheckedItems
ItemCheckArea = Check
ListSeparator = ,
I am trying to restrict the user from un-checking all the items so that at least one item in the drop down remains selected. For this I have hooked to SelectionChange event and have written a handler something like this...
if (ultraComboEditor1.SelectedItem.CheckState == CheckState.Unchecked && ultraComboEditor1.CheckItems.Count == 0)
{
ultraComboEditor1.SelectedItem.CheckState == CheckState.Checked;
}
But I have noticed that SelectionChanged event is not fired everytime we check/uncheck an item in the dropdown list, and hence I am able to enforce the above restriction.
I have also tried SelectionChangeCommitted event, but did not help.
I am not sure if I am missing something here, but it seems like these events are not fired every time a selection is changed.
Is there any other way to have atleast one item selected?
When the value/text of the UltraComboEditor has changed either by a user typing in a value or selecting from the drop down, the ValueChanged event fires.
Give that event a try instead of the SelectionChanged event. Also, if your check boxes start out as all unchecked, you will need to check if the SelectedItem property is null, and call return to avoid an exception.
Thanks Torrey for the pointer on ValueChanged event.
I tried this event and it did work for me with the following snippet:-
List<object> selectedItems = new List<object>(); private void ultraComboEditor1_ValueChanged(object sender, EventArgs e) { if (ultraComboEditor1.CheckedItems.Count == 0) { foreach (var item in ultraComboEditor1.Items) { if (ReferenceEquals(item.ListObject, selectedItems[0])) { item.CheckState = CheckState.Checked; } } } selectedItems.Clear(); foreach (var checkedItem in ultraComboEditor1.CheckedItems) { selectedItems.Add(checkedItem.ListObject); } }
But note that, I have to maintain an external list to keep track of previously selected items which is used to re-check the last unchecked item. We cannot rely on SelectedItem member here since it may not actually refer to the item currently being checked/unchecked.
It would have been nice if UltraComboEditor had also fired ItemCheck event, just like Windows Forms CheckedListBox, with event arguments having the previous and the new CheckState for the item.