I encountered a pretty interesting situation this morning as I am working on a utility containing a ListView control in Visual Studio 2003.
I have a Start button that I would like to be disabled until the user actually selects items from the ListView by clicking on a checkbox associated with the list item.
The ListView has a list items collection called .CheckedItems that contains the list of items that have been checked inside the ListView. The problem surfaces because the update of this collection takes a bit of time and if I am using the .CheckedItems.Count to enable or disable my start button, it will produce a very inconsistent user experience depending on how quickly the user clicks the checkboxes.
To get around this problem, you actually need to create a variable to store the number items that have been checked. Starting off at zero, of course, and incrementing or decrementing the value each time the user clicks an item's checkbox.
The code to do this will go into the ItemCheck event, as shown below:
private void listAvailable_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
{
if(e.NewValue==CheckState.Checked)
iCheckedCount++;
else
iCheckedCount–;
btnStart.Enabled = (iCheckedCount > 0);
}
Hopefully, this will save you a bit of time should need to implement the same practice.
Leave a reply