protected override void OnLostFocus(EventArgs args) { if (!ContainsFocus) {
The trick is to check ContainsFocus
In your constructor, you may have to add code similar to the following to capture the lost focus of your child controls (since you will not receive direct notification when they lose focus elsewhere), causing
CaptureLostFocus(this);
and implementations:
void CaptureLostFocus(Control control) { foreach(Control child in control.Controls) { child.LostFocus += (s, e) => OnLostFocus(e); CaptureLostFocus(control); } }
source share