C# WinForms AcceptButton 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/487920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:20:55  来源:igfitidea点击:

WinForms AcceptButton not working?

c#winformsmodal-dialog

提问by Svish

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 dialog like so:

我做了两个表格。第一个表单上只有一个简单的按钮,它以对话框的形式打开另一个,如下所示:

using (Form2 f = new Form2())
{
    if (f.ShowDialog() != DialogResult.OK)
        MessageBox.Show("Not OK");
    else
        MessageBox.Show("OK");
}

The second, which is that Form2, has two buttons on it. All I have done is to set the forms AcceptButton to one, and CancelButton to the other. In my head this is all that should be needed to make this work. But when I run it, I click on the button which opens up Form2. I can now click on the one set as CancelButton, and I get the "Not OK" message box. But when I click on the one set as AcceptButton, nothing happens? The InitializeComponent code of Form2 looks like this:

第二个,也就是Form2,上面有两个按钮。我所做的只是将表单 AcceptButton 设置为一个,将 CancelButton 设置为另一个。在我看来,这就是完成这项工作所需的一切。但是当我运行它时,我点击了打开 Form2 的按钮。我现在可以单击一个设置为 CancelButton 的按钮,然后我收到“Not OK”消息框。但是当我点击一个设置为 AcceptButton 时,没有任何反应?Form2 的 InitializeComponent 代码如下所示:

private void InitializeComponent()
{
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(211, 13);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.UseVisualStyleBackColor = true;
    // 
    // button2
    // 
    this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
    this.button2.Location = new System.Drawing.Point(130, 13);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(75, 23);
    this.button2.TabIndex = 1;
    this.button2.Text = "button2";
    this.button2.UseVisualStyleBackColor = true;
    // 
    // Form2
    // 
    this.AcceptButton = this.button1;
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.CancelButton = this.button2;
    this.ClientSize = new System.Drawing.Size(298, 59);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Name = "Form2";
    this.Text = "Form2";
    this.Load += new System.EventHandler(this.Form2_Load);
    this.ResumeLayout(false);
}

I have done nothing else than add those two buttons, and set the AcceptButton and CancelButton. Why doesn't it work?

除了添加这两个按钮,并设置 AcceptButton 和 CancelButton 之外,我什么也没做。为什么不起作用?

采纳答案by Martin Moser

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.

仅仅设置 AcceptButton/CancelButton 是不够的。这只是告诉应该在Enter/上调用哪个按钮Esc。您必须在 Button 处理程序中设置 DialogResult。

回答by Samuel

Try setting DialogResulton button1

尝试设置DialogResultbutton1

this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;

回答by Ads

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:

我遇到了 AcceptButton 无法正常工作的问题,虽然 DialogResult 建议是修复的一部分,但我还有另外两件事需要更改:

  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.
  1. 我的按钮不可见 - 故意因为我想在通过扫描条形码“按下”回车时停止“叮”。
  2. 按钮所在的容器有所不同。我必须将它放在同一个容器中,在我的例子中是 Forms.Panel,作为试图访问它的文本框。我不确定为什么这会有所作为,但确实如此。

I hope this helps someone.

我希望这可以帮助别人。

回答by Khaled Eleftawi

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

您需要将表单的 KeyPreview 属性设置为 True,默认值为 False。请记住,如果焦点设置为任何其他按钮而不是 AcceptButton,则 Enter 键将执行此按钮