I ran into a situation where I needed to know what control was gaining focus in the Leave() event of another control. The situation I was working with is as follows; I have a multi-line text box that when it gains focus I want a listview of potential values to popup below it. When the textbox loses focus, the listview should be hidden. The user should be able to click on the listview to select items to input into the textbox. The problem was when the user clicked on the listview, the textbox lost focus and was hiding the listview.
Enter the ActiveControl property on the form. This handy property allows me to check, in the Leave() event, what is the new active control with focus. Thus I can check to see if the textbox lost the focus to the listview control and if so, skip the hiding of it.
Here we have the textbox named txtComplaint, when it gets the focus we make listView visible.
private void txtComplaint_Enter(object sender, System.EventArgs e)
listView.Visible = true;
When the textbox loses focus we see if the ActiveControl is the listView, if not we hide it.
private void txtComplaint_Leave(object sender, System.EventArgs e)
if (ActiveControl.Name != "listView")
treeComplaint.Visible = false;