Can't Find GotFocus()/LostFocus() in My User Control
c#, user-controls, winforms
Solution
It looks like from the MSDN Documentation that they are there inherited from Control, but are not encouraged to be used. They want you to use the Enter and Leave Events.
Note 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.
That said you can get access to them as User1718294 suggested with the += or you can Override the `OnGotFocus` and `OnLostFocus` Event.
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
}
Problem
I've created a WinForms User Control. I read a couple of places something about `GotFocus()` and `LostFocus()` events, yet my user control doesn't provide these events in the Events part of the Properties window. I even tried typing `override` to see if these event handlers would come up but they don't. I can't find them anywhere. So I created my own methods with these names, and then I get the following error: Warning 1 'mynamespace.mycontrol.GotFocus()' hides inherited member 'System.Windows.Forms.Control.GotFocus'. Use the new keyword if hiding was intended. What the heck is going on here. If `GotFocus()` already exists, why can't I find it and use it?