Cancel ObjectLostSelection event

Hi

I perform some validation in the ObjectLostSelection event. If this validation fails I display a message box error and programmatically reselect the node, using:
myView.Selection.Select(deselectedStep);
My problem is if I use the code above in the ObjectLostSelection event, it fires the ObjectLostSelection event again for the same node. This results in the message box displaying twice.
Is there anyway to simply cancel the event rather than reselecting node? or is this a bug?

That’s not a bug, because you aren’t supposed to modify the GoSelection collection during the ObjectLostSelection event.

It is documented for the GoObject.OnLostSelection method, but that documentation is missing for the GoView.ObjectLostSelection event. So that is an omission in our documentation -- sorry about that!
The reason for the behavior that you see, and the reason for the restriction that changing the selection in an ObjectLostSelection event handler is unsupported, is that when the user clicked in the background of the view in order to deselect the selected object, the tool calls GoSelection.Clear(). This in turn calls Remove for each selected object, which raises the ObjectLostSelection event. But your code then added an object back into the collection, which caused GoSelection.Clear() to remove it again.
The easiest solution might be to make sure the object doesn't get removed from the GoSelection in the first place. This requires a custom GoSelection class as well as a custom GoView class:
public class SpecialSelection : GoSelection {
public SpecialSelection(GoView view) : base(view) {}
// prevent the selected object from losing selection, maybe
public override void Remove(GoObject obj) {
IGoLabeledPart node = obj as IGoLabeledPart;
if (node != null && node == this.Primary && node.Text.Contains("e")) {
MessageBox.Show("Hey! Selected IGoLabeledPart.Text contains the letter 'e'.");
} else {
base.Remove(obj);
}
}
public class SpecialView : GoView {
public SpecialView() {}
public override GoSelection CreateSelection() {
return new SpecialSelection(this);
}
}
Caution! This code handles multiple selections by only checking the object that is the Primary selection. It won't work if you were to check additional selected objects. Although doing so would be too confusing to the user anyway.
A comment: as a user I would find this style of validation to be very annoying.