WinForms AcceptButton not working?

前端 未结 5 409
生来不讨喜
生来不讨喜 2021-01-17 09:34

Ok, this is bugging me, and I just can\'t figure out what is wrong...

I have made two forms. First form just has a simple button on it, which opens the other as a di

相关标签:
5条回答
  • 2021-01-17 09:52

    Try setting DialogResult on button1

    this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
    
    0 讨论(0)
  • 2021-01-17 09:59

    I had an issue with the AcceptButton not working and while the DialogResult suggestion was part of the fix, I had 2 other things that needed to change:

    1. My button was not visible - Intentionally because I wanted to stop the "ding" when a carriage return was "pressed" by scanning a barcode.
    2. The container that the button was inside made a difference. I had to have it in the same container, in my case a Forms.Panel, as the textbox that was trying to access it. I'm not sure why this would make a difference, but it did.

    I hope this helps someone.

    0 讨论(0)
  • 2021-01-17 10:04

    Just setting the AcceptButton/CancelButton is not enough. This just tells which button should be invoked on Enter/Esc. You have to set the DialogResult in the Button handler.

    0 讨论(0)
  • 2021-01-17 10:11

    You need to set the KeyPreview property of the form to True, the default value is False. Remember that if focus is set to any other button rather than the AcceptButton the Enter key will execute this button

    0 讨论(0)
  • 2021-01-17 10:12

    Definitely try the tutorial How to easily apply AcceptButton and CancelButton for custom dialog box in Winform.

    0 讨论(0)
提交回复
热议问题