的WinForms的AcceptButton不工作? [英] WinForms AcceptButton not working?

查看:125
本文介绍了的WinForms的AcceptButton不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是窃听我,我只是无法弄清楚什么是错的...

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");
}



第二,这是窗体2 ,上有两个按钮。我所做的是设置形式的AcceptButton一个,并CancelButton到其他。在我的头,这是所有应需要,使这项工作。但是,当我运行它,我点击开辟了窗体2按钮。我现在可以点击一个设置为CancelButton,我也得到了不正常的消息框。但是,当我点击一组为的AcceptButton,没有任何反应?
的Form2在InitializeComponent代码如下所示:

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:

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。为什么不工作?

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

推荐答案

刚刚设置的AcceptButton / CancelButton是不够的。这只是告诉了哪个按钮应该在<大骨节病>输入 / <大骨节病> ESC 被调用。你必须设置在按钮处理程序的DialogResult。

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.

这篇关于的WinForms的AcceptButton不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆