I'm trying to handle the user changing which items are selected in a listbox (by updating information about what's selected), but if you select a range (using shift+select), it actually fires a separate 'ItemSelectionChanged' event onc开发者_开发问答e for EACH item that was selected/deselected, i.e. if you selected 100 items, you get 100 events, and the first time the event handler's called, it seems to have no way of knowing that there's more to come.
Is there a way to not respond until the process of selecting/deselecting items is complete?
There is no SelectedItemsChanged event, I'm guessing you mean SelectedIndexChanged. What you can do is leverage the Control.BeginInvoke() method. The delegate target starts running when the UI thread goes idle again, after all the events have been fired. Make it look like this:
bool listUpdated = false;
private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
if (!listUpdated) {
this.BeginInvoke(new MethodInvoker(updateList));
listUpdated = true;
}
}
private void updateList() {
listUpdated = false;
// etc...
}
精彩评论