How to determine which control on form has focus?

48,467

Solution 1

I've found a solution which appears to be working.

    private void DeviceForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == 13 && tstxtDeviceFilter.Focused)
        {
            filterByDeviceSN();
        }
    }

I can't help but think there must be a better way though!

--EDIT--EDIT--EDIT--EDIT--EDIT--

Well, after looking at the suggestions below (thank you) I've found a 'better' way for me in this circumstance.

    this.tstxtDeviceFilter.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tstxtDeviceFilter_KeyDown);

    private void tstxtDeviceFilter_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == 13)
        {
            filterByDeviceSN();
        }
    }

Obviously by trapping the event on the textbox itself rather than the form I don't need to worry about focus. Once again I feel dumb for not thinking of that for so long!

Solution 2

Each form has a property for an "Accept" button & "Cancel" button, these are the buttons that get "clicked" when the user presses enter and escape respectively.

You can change the default button as each control gets the focus (you can have one got focus event hander per button, and share it with a set of text boxes)

If you do this then the apperance of the buttons change giving the user a visual cue telling them which button is the default.

Alternatively, if you don't want to do that, you can use the "ActiveControl" property, and test to see which of the sets of text boxes it belongs to.

Have you asked yourself, what should the default button be if it's not one of thse text boxes?

Solution 3

Have you tried Form.ActiveControl?

Share:
48,467
G-.
Author by

G-.

Newbie programmer. Learning fast!

Updated on July 18, 2022

Comments

  • G-.
    G-. almost 2 years

    I've read elsewhere on here that to capture "Enter" key stroke in a text box and use it as if pushing a button I should set the KeyPreview property of the form to true and check the value of KeyDown.

    I want to be able to use this functionality on several TextBox controls which each are associated with a different Button.

    My question is how do I know which control caused the KeyPress event? The sender is listed as the form itself.

    G

  • G-.
    G-. about 15 years
    ActiveControl.Name returns an empty string. ActiveControl.Text behaves as expected by returning the same string as is currently in the textbox though. "this.ActiveControl.Equals(tstxtDeviceFilter)" returns false. Think I'll go with my own answer for now. Unless anyone knows how to get the name?
  • G-.
    G-. about 15 years
    Thanks BW, this is useful information to know. I think I'll go with my own answer in this case as I'm only interested in "Enter" key presses in the single textbox and only when it has the focus. Thanks tho!
  • Matthias Wandel
    Matthias Wandel over 12 years
    G-'s comment clued me in where I was going wrong, thanks for more detail. My app is mostly just this one dialog, so it doesn't even have a button and never closes except when you quit.
  • colin lamarre
    colin lamarre almost 5 years
    i guess it doesn't always work, ActiveControl.Focused is False, this case a splitContainer.