开发者

C# - Simple Validation - DialogResult

开发者 https://www.devze.com 2023-01-18 09:10 出处:网络
I have the following code for a button click on a form: private 开发者_如何转开发void btnOK_Click(object sender, EventArgs e)

I have the following code for a button click on a form:

        private 开发者_如何转开发void btnOK_Click(object sender, EventArgs e)
        {

        if (this.txtProjectName.Text == "")
        {
            MessageBox.Show("No project name entered", "No Project Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            btnOK.DialogResult = DialogResult.None;
        }
        else
        {
            this.btnOK.DialogResult = DialogResult.OK;
            return;
        }
    }

If there is something in the text box, the form will only close on the second click. Is there a way to close the form instantly, and pass a DialogResult.OK to it's caller?

Thanks


Instead of setting the this.btnOK.DialogResult, use this:

this.DialogResult = DialogResult.OK;

This will set the DialogResult of the Form. The form will close, and DialogResult will have its correct value.

0

精彩评论

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