OnLostFocus is called after dispose is called in a dataGridView
I Have a control inheriting the dataGridView control. I have the onLostFocus method overrided. Lately I encountereda weird behavior. if trying to close the form while a cell is in teh middle of being edited. the dispose method will be called and then teh onLostFocus is called that results in a nullReferenceException
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
base.DefaultCellStyle = myStyle1;
}
}
my question is how come the lostFocus is called after the userControl starts being disposed? and what is the correct way to handle this isuue? A workaround can be to check explicitly if dispose had started and then return from the OnLostFocus. But I'd rather understans better what hap开发者_开发技巧pens behind. Thanks!
According to http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx, Microsoft suggested that OnEnter
and OnLeave
should be used instead of OnGotFocus
and OnLostFocus
.
The GotFocus and LostFocus events are low-level focus events that are tied to the WM_KILLFOCUS and WM_SETFOCUS Windows messages. Typically, the GotFocus and LostFocus events are only used when updating UICues or when writing custom controls. Instead the Enter and Leave events should be used for all controls except the Form class, which uses the Activated and Deactivate events. For more information about the GotFocus and LostFocus events, see the WM_SETFOCUS and WM_KILLFOCUS topics in the "Keyboard Input Reference" section in the MSDN library at http://msdn.microsoft.com/library.http://msdn.microsoft.com/library.
精彩评论