Find out the control with last focus

There’s no built in property or functionality for keeping track of the previous-focused control. As you mentioned, whenever the button is clicked, it will take the focus. If you want to keep track of the textbox that was focused before that, you’re going to have to do it yourself.

One way of going about this would be to add a class-level variable to your form that holds a reference to the currently focused textbox control:

private Control _focusedControl;

And then in the GotFocus event for each of your textbox controls, you would just update the _focusedControl variable with that textbox:

private void TextBox_GotFocus(object sender, EventArgs e)
{
    _focusedControl = (Control)sender;
}

Now, whenever a button is clicked (why are you using the MouseDown event as shown in your question instead of the button’s Click event?), you can use the reference to the previously-focused textbox control that is saved in the class-level variable however you like:

private void button1_Click(object sender, EventArgs e)
{
    if (_focusedControl != null)
    {
        //Change the color of the previously-focused textbox
        _focusedControl.BackColor = Color.Red;
    }
}

Leave a Comment