views:

2280

answers:

4

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:

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?

+10  A: 

Try setting DialogResult on button1

this.button2.DialogResult = System.Windows.Forms.DialogResult.OK;
Samuel
+7  A: 

just setting the AcceptButton/CancelButton is not enough, this just tell which button should be invoked on Enter/ESC, you have to set the Dialogresult in the Button handler.

Martin Moser
What is quite strange is that setting the CancelButton actually sets that button's DialogResult to Cancel, but AcceptButton doesn't do the same for that button.
Carl
Aaaaah. Thank you!! This reeaally bugged me, haha. You were absolutely correct. the cancel button had its dialogresult set to cancel, but the other one didn't. Didn't think of that dialogresult property at all... oh well. Thank you again :)
Svish
The dialog result is set to cancel because its the default value
Martin Moser
A: 

samuel it doesn't work properly for me !!!!!!!are you sure it works fine.

Punit
we hav to give proper tab index too for our controls, hav a look at hte below link for ref-http://social.msdn.microsoft.com/forums/en-US/winforms/thread/28f46119-777d-4d4b-be62-c4b277574efd/
Punit