わかりました、これは私を悩ませています、そして私は何が悪いのか理解できません...
私は2つのフォームを作成しました。最初のフォームには単純なボタンがあり、次のようなダイアログとして他のフォームを開きます。
using (Form2 f = new Form2())
{
if (f.ShowDialog() != DialogResult.OK)
MessageBox.Show("Not OK");
else
MessageBox.Show("OK");
}
2つ目は、Form2
2つのボタンがあります。私が行ったのは、フォームのAcceptButtonを一方に設定し、CancelButtonをもう一方に設定することだけです。私の頭の中では、これがこの作業を行うために必要なすべてです。しかし、それを実行するときは、Form2を開くボタンをクリックします。これで、CancelButtonとして設定されたものをクリックすると、「NotOK」メッセージボックスが表示されます。しかし、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);
}
これらの2つのボタンを追加し、AcceptButtonとCancelButtonを設定する以外に何もしていません。なぜそれが機能しないのですか?