C # - Simple check - DialogResult

I have the following code for clicking a button 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 be closed only the second time it is clicked. Is there a way to instantly close a form and pass it to DialogResult.OK?

thank

+3
source share
1 answer

Instead of installing, this.btnOK.DialogResultuse the following:

this.DialogResult = DialogResult.OK;

This will set the DialogResultforms. The form will be closed and DialogResult will have the correct value.

+12
source

Source: https://habr.com/ru/post/1767325/


All Articles