views:

2313

answers:

5

Hi,

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

A: 

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!

G-
A: 

Hi there,

would this help with what you are looking at?

Blounty
Afraid not, that seems to be talking about ASP web pages with reference to JavaScript code. I don't speak Java (yet) as I don't think I need it for .Net win forms development. I chose the "winforms" tag but is the etiquette on here to mention those bits in the body as well?
G-
@G: Nitpick: Java is to Javascript as Car is to Carpet.
Andrew Coleson
@A. Coleson - Thanks, hadn't really thought about it (newbie here) but I'm a bit of nitpicker myself so that's good to know :-)
G-
+1  A: 

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?

Binary Worrier
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!
G-
+1  A: 

Have you tried Form.ActiveControl?

mjmarsh
I had a look at this but where I would expect to find the name of the control with the focus "this.ActiveControl.Name" was an empty string. I'm probably doing something very obviously wrong though!
G-
A: 

You could use this code as a starting point to capture the key down events of the form. The ActiveControl is one that has the focus. In this example, it's flexible for adding other actions on "Enter" when you are in different TextBoxes on the form. It's VB.NET, but you should be able to easily convert to C#.

Private Sub MyForm_KeyDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    If e.KeyCode = Keys.Enter Then
        If Me.ActiveControl.Name = Me.TextBox1.Name Then
            ' This is the TextBox we want to be active to run filterByDeviceSN()
            filterByDeviceSN()
        ElseIf Me.ActiveControl.Name = Me.TextBox2.Name Then
            foo()
        End If
    End If
End Sub
HardCode
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-