-1

My program compiles and runs fine as long as you only use the mouse to navigate. I noticed that when I hit "Enter" it automatically registers as clicking one of my buttons in the window. I have started playing around with the "AcceptButton" property and setting it to appropriate buttons or even to "None." Nothing seems to work and it stays with it's default button it seems to has tied to "Enter." I have noticed that the buttons it's going to are the first I have defined in the code.

Long story short, I want to remove the "default" value for the Enter key to what the "AcceptButton" property actually specifies it to be.

Thanks,

Andy

Bob.
  • 3,894
  • 4
  • 44
  • 76
Andy
  • 1
  • 2
  • 2
  • is this a windows forms app? – DROP TABLE users Mar 28 '13 at 16:45
  • Don't make the button with `TabIndex = 0`. Go to "View" - "Tab Order" to change the order of your controls. – LarsTech Mar 28 '13 at 16:56
  • Are you wanting to Call the same Method when the enter key is Hit on that the `AcceptButton` Click_Event..? if so then just assign in your KeyPressEvent Check for `e.KeyCodes` to be Return or Enter Key If so then assign the EventHandler there or create a delegate – MethodMan Mar 28 '13 at 16:59

2 Answers2

0

you could capture the onKeyDown event and not handle it if it is enter

protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        this.YOURBUTTON.PerformClick();
    }
}
DROP TABLE users
  • 1,955
  • 14
  • 26
  • I have tried a segment of code that looked similar "OneKeyDown" one. I changed the the TabIndex properties of my buttons, so that the one I wanted to select is now '0'. Works as I intended, but I still don't really get why the "AcceptButton" property didn't work. THANK YOU FOR THE HELP :D – Andy Mar 28 '13 at 17:02
  • was one of the buttons set to tab index of zero already when you were trying to set the `acceptButton` proberty? – DROP TABLE users Mar 28 '13 at 17:05
  • Yeah, does that override the property? – Andy Mar 28 '13 at 17:07
  • I guess so I have never used that accept button property. I edited my answer to something that might work for you without doing the `tabIndex` workaround, or the accepButton thing. So if the user hits enter you can tell it to click the button that you want – DROP TABLE users Mar 28 '13 at 17:10
  • If it had TabIndex of zero then it will have focus when you load the form. Clicking enter clicks the focused button. – James Mar 28 '13 at 17:21
0

Assuming the WinForm has a TextBox, set the TextBox.TabIndex to 0. Again, making the assumption that this TextBox should be the first UI Element the user interacts with.

Then, change all the buttons to have a TabIndex > 0.

Finally, update the Form.AcceptButton to be the button you want to have for the default Accept/Enter.

If there is not a TextBox or some other element that can have a lower TabIndex, then the button will be the default UI Element with focus when the form is loaded up.

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140