开发者

Why do buttons need to be pressed twice to work in this modal dialog?

开发者 https://www.devze.com 2022-12-13 03:48 出处:网络
I have this C# code: public partial class Continue : Form { public Continue(string colourName) { InitializeComponent();

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)
    {
        btnConfir开发者_运维技巧m.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消