AcceptButton not working
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:
this.AcceptButton
{Text="No"}
[System.Windows.Forms.Button]: {Text="No"}
DialogResult: None
Any thoughts/ideas?
[756 byte] By [
Sue0312] at [2007-12-22]
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.