【问题标题】:WinForms AcceptButton not working?WinForms AcceptButton 不起作用?
【发布时间】:2009-01-28 15:00:37
【问题描述】:

好吧,这让我很烦,我就是不知道出了什么问题......

我做了两个表格。第一个表单只有一个简单的按钮,它可以像这样以对话框的形式打开另一个:

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

第二个,即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);
}

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

【问题讨论】:

    标签: c# winforms modal-dialog


    【解决方案1】:

    仅设置AcceptButton/CancelButton 是不够的。这只是告诉应该在 Enter/Esc 上调用哪个按钮。您必须设置按钮的DialogResult 属性。

    【讨论】:

    • 比较奇怪的是,设置 CancelButton 实际上是将该按钮的 DialogResult 设置为 Cancel,但 AcceptButton 并没有对那个按钮做同样的事情。
    • 啊啊啊。谢谢!!这真的让我很烦,哈哈。你完全正确。取消按钮的对话框结果设置为取消,但另一个没有。根本没有想到那个 dialogresult 属性......哦,好吧。再次感谢你:)
    • 对话结果设置为取消,因为它是默认值
    • 只有我,还是只有在 DialogResult 设置为 Ok 的情况下,Accept 按钮才会关闭表单?
    • 是的,接受按钮也会关闭表单@AndriyK
    【解决方案2】:

    尝试在button1 上设置DialogResult

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

    【讨论】:

    • 我比接受的答案更喜欢你的答案,因为你确切地指定了需要做什么。 (除了混淆 button1 与 button2 :-)
    【解决方案3】:

    【讨论】:

    • 看起来 DialogResult 应该在点击 sub 之前设置为 OK。
    【解决方案4】:

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

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

    我希望这对某人有所帮助。

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 它可以使用 false KeyPreview 属性。但对我来说,TabIndex 属性决定了应该按下哪个按钮
      猜你喜欢
      • 2018-01-18
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2011-06-21
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多