I am setting the default button on my form, like so:
this.AcceptButton = this.noButton;
But the other button ("Yes") button click is still being envoked. In the designer the noButton appears to be set as the default (it has an outline). Then when I run, the"Yes" button has an outline and acts as the default. The debugger also seems to think that the noButton is the default button:
Click on another control on your form - a textbox or radio button or something. I suspect you'll find that your No button gets outlined as you expect.
The AcceptButton identifies the button that should be 'clicked' if someone types enter in some other control (like a textbox). However, the control that has focus when the form is first displayed is the control with a tabindex value of zero. So, if your "yes" button has tab index zero, that's the button that will have focus when the form is first displayed.
If you press enter while a button has focus that's treated as you clicking on the focused button - which takes precedence over the AcceptButton behaviour .
Either set your no button's tabindex to zero or set some other non-button control's tabindex to zero and I'm guessing you'll get the behaviour you want.
I am seeing the same problem.
I set my form's AcceptButton, I call the button's NotifyDefault, and I call Focus() on the button...
and it displays with the wrong button highlighted and the wrong button is fired upon <Enter>. I've rearranged TAB order to no avail.
Oddly, I've discovered if I make subsequent calls to the form's ShowDialog method, then the screen looks right, but the wrong button still gets fired.
And, as in your case, the debugger says the AcceptButton is the one I want.
I can click all over the form and nothing changes... UNLESS I click on the desired button... in which case subsequent calls to ShowDialog work as expected... the desired button is highlighted and that button fires when <Enter> is pressed.
I'm using Visual Studio 2005 / .NET 2.0 Is there a known problem or is there some other voodoo required?
DUH - If I programatically change the tab index with the assignment of AcceptButton I get the result I need.
The answer was in the reply... I was just a little slow in digesting it. Thanks. My form is essentially a MessageBox - it contains only an icon, a label and a couple buttons. The buttons are the only active controls on the form, so one of them always had tab order 0, so the other would never be the working AcceptButton. I don't understand why the call to desiredButton.Focus() didn't take care of it... but I won't worry about it. I've already spent far too long on this!