WinForms AcceptButton not working?

Well, that annoys me, and I just can't figure out what's wrong ...

I made two forms. The first form just has a simple button that opens another in the form of a dialog:

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

The second, which consists of Form2 , has two buttons on it. All I did was set the AcceptButton forms to one and CancelButton to the other. In my head, this is all that is needed to carry out this work. But when I start it, I click the button that opens form 2. Now I can click on one of them as CancelButton, and I get the message "Not OK." But when I click on the one that is set as AcceptButton, nothing happens? The InitializeComponent form 2 code is as follows:

 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 did nothing but add these two buttons, and set AcceptButton and CancelButton. Why is this not working?

+33
c # winforms modal-dialog
Jan 28 '09 at 15:00
source share
4 answers

Just setting AcceptButton / CancelButton is not enough. It just says which button should be called on Enter / Esc . You must set DialogResult in the Button handler.

+52
Jan 28 '09 at 15:05
source share

Try setting DialogResult to button1

 this.button1.DialogResult = System.Windows.Forms.DialogResult.OK; 
+45
Jan 28 '09 at 15:05
source share
+3
May 21 '10 at 2:52
source share

I had a problem with AcceptButton not working, and although the DialogResult suggestion was part of the fix, I had 2 more things that needed to be changed:

  • My button was not visible - Intentionally, because I wanted to stop "ding" when the carriage return "pressed" by scanning the barcode.
  • The container with the button inside changed the situation. I had to have it in one container, in my case Forms.Panel, as a text field 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
Nov 06 '16 at 23:24
source share



All Articles