Reputation: 23833
I have a basic custom dialog box for use with various controls in a WinForms application. The dialog looks like:
For various reasons I do not want the user to have the ability to use the Enter
key to select the 'Yes' option (buttonYes
). Previously I did want this behaviour and I set the AcceptButton
property of the Yes button (buttonYes
) accordingly. I have scince removed this, setting buttonYes
's AcceptButton
property to 'None', but the form still fires the buttonYes.Click
event when the Enter
key is pressed. I have also tried to handle the KeyPress
or KeyDown
events but these are not being fired when the Enter
key is used. This is basic and annoying, has anyone come across this and what can I do to implement the functionality I want?
Upvotes: 5
Views: 19100
Reputation: 1
I try any methods to do this, but work for me one:
Delete AcceptButton.
Make a event for click on your button.
Write in event following code:
this.DialogResult = DialogResult.OK;
Upvotes: 0
Reputation: 1
Simple Solution I tried many of the above solutions. This is what worked for me.
In InitializeComponent, ensure that the AcceptButton is not set.
Delete, or comment out, this code:
// this.AcceptButton = this.btnClose;\
And on the button, add:
this.btnClose.TabStop = false; (somebody mentioned this one above)
If that doesn't work, you can also try moving the focus off of the button, and to the window frame. this.Focus()
Upvotes: 0
Reputation: 11
I just had to deal with this and none of the solutions offered worked so I handle Click event like:
private void yes_Click( object sender, EventArgs e )
{
if( e.Equals( EventArgs.Empty ) )
return; // ignore "Enter" key press
// process mouse click...
}
Upvotes: 1
Reputation: 9535
All the answers here are wrong or have the wrong emphasis.
In order to understand what is going on you have to understand how Form.ShowDialog(...) is implemented, basically. Under the hood, ShowDialog is going into its own message loop. That message loop has hard-coded logic that treats the Enter key specially.
The dialog isn't closing because the OK button has focus, an enter press happens, and WinForms executes the OK button's click logic, which closes the window. The dialog is closing because the Enter press message is being plucked out of a message loop that is running on the top of the call stack, consumed, and causing ShowDialog to break out of its loop and close the window ... so you can't get the functionality you want by handling the key press events on the button; The key press event for Enter is never dispatched. The events going through KeyPreview are just another way of getting at the same events, and so this also doesn't help.
The only thing that works is to set the form's AcceptButton property to (none). The OK button can still have its DialogResult property set to OK so doing this will not break the dialog, but with AcceptButton set to none, WinForms just ignores Enter clicks from within ShowDialog.
Upvotes: 14
Reputation: 6651
To fix this, set the TabStop
property to False
and use the code below:
private void form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
e.Handled = true;
}
Or, if as you say, you are not able to break at KeyPressEvent you need to handle ProcessKeyPreview
protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m)
{
int _ENTER = 13;
if (m.Msg == _ENTER)
{
//Do nothing
}
return base.ProcessKeyPreview(ref m);
}
Upvotes: 8
Reputation: 135
You should take a look at the *.resx or *.ressources file.
Search for the AcceptButton
property. It may have been set in one of these files.
Upvotes: 0
Reputation: 5890
Since you've already changed the Accpet button property: what about form's KeyPreview property? Is it set to false or true? Maybe you set it to true somewhere along the way and forgot about it. Set it to false.
Upvotes: 1
Reputation: 5757
Also, if you want to ensure users will have to click on the buttons, instead of using the keyboard, you could set the TabStop
property to False
on the buttons.
Upvotes: 4