views:

61

answers:

1

I have this C# code:

public partial class Continue : Form
{
    public Continue(string colourName)
    {
        InitializeComponent();
        lblMessage.Text = String.Format("Do you wish to change the colour to {0}", colourName);
    }

    private void btnConfirm_Click(object sender, EventArgs e)
    {
        btnConfirm.DialogResult = DialogResult.OK;
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        btnCancel.DialogResult = DialogResult.Cancel;
    }
}

It works fine, but when the dialog pops up it requires me to click on a button twice to use it. Does anyone have an idea why?

+4  A: 

You need to set this.DialogResult rather than btnxxx.DialogResult in the Click handlers, or set the btnxxx.DialogResult before the handlers.

The form's DialogResult is set to the button's before the Click method is run, so the first time the event is run the Form's DialogResult is left at None, but the second time it is set to the (now-set) button's DialogResult.

Rawling
Perfect, thanks!
Ross