protected virtual void CleanupCellEdit(bool expectingCellEdit)
{
if (this.cellEditor == null)
return;
this.cellEditor.Validating -= new CancelEventHandler(CellEditor_Validating);
Control soonToBeOldCellEditor = this.cellEditor;
this.cellEditor = null;
// Delay cleaning up the cell editor so that if we are immediately going to
// start a new cell edit (because the user pressed Tab) the new cell editor
// has a chance to grab the focus. Without this, the ListView gains focus
// momentarily (after the cell editor is remove and before the new one is created)
// causing the list's selection to flash momentarily.
EventHandler toBeRun = null;
toBeRun = delegate(object sender, EventArgs e) {
Application.Idle -= toBeRun;
this.Controls.Remove(soonToBeOldCellEditor);
this.Invalidate();
if (!this.IsCellEditing) {
this.Select();
this.PauseAnimations(false);
}
};
// We only want to delay the removal of the control if we are expecting another cell
// to be edited. Otherwise, we remove the control immediately.
if (expectingCellEdit)
this.RunWhenIdle(toBeRun);
else
toBeRun(null, null);
}