Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine which control on form has focus?

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

like image 538
G-. Avatar asked Feb 04 '09 17:02

G-.


People also ask

What is a control focus?

Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.

Which method is used to set input focus to the control?

Use the SetFocus method when you want a particular field or control to have the focus so that all user input is directed to this object. To read some of the properties of a control, you need to ensure that the control has the focus. For example, a text box must have the focus before you can read its Text property.

How do you place the controls on a form?

Add the control by drawing Select the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.

What is the focus in Visual Studio?

The word "Focus" means that a particular control (e.g. a textbox) is in focus or not. When a user clicks on/in a control like textbox then this control gets all the input (through the keyboard) from the user.


2 Answers

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?

like image 117
Binary Worrier Avatar answered Oct 06 '22 00:10

Binary Worrier


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!

like image 35
G-. Avatar answered Oct 06 '22 01:10

G-.