Closing the dialog box and the forms that launched the dialog box

I have a form in a winforms application. When I click the button, it loads a modal message box with the yes and no options.

This is good, but when I click no, I want to close both the dialog box and the form where the button that launched the dialog box (sender) is.

So, the structure of the application looks like this:

Main application window> click the menu item to launch a new form (connection setup)> click the button in this form to launch the message window.

Two windows are open (connection setup form and dialog box) that I want to close.

How can i do this?

+3
source share
4 answers

yes-no DialogResult "", "", :

private void noButton_Click(object sender, EventArgs e)
{
    this.DialogResult = System.Windows.Forms.DialogResult.No;
}

, No

, , - ( ):

var modalForm = new YesNoForm();
if (modalForm.ShowDialog() == DialogResult.No)
{
    this.Close(); // close the connection setup form
}

, "-" , MessageBox, :

var dlgResult = MessageBox.Show("Yes or no ?","?",MessageBoxButtons.YesNo);
if(dlgResult == System.Windows.Forms.DialogResult.No)
{
    this.Close(); // close the connection setup form
}

+16

- :

DialogResult result = MessageBox.Show("dialog", "modal", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
      this.Close();
}

.

+2
 private void newToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (richTextBox1.Text != "")
        {

            if (DialogResult.Yes == MessageBox.Show(("Do you want to save changes to Untiteled"), "Notepad", MessageBoxButtons.YesNoCancel))
            {

                saveFileDialog1.ShowDialog();
                FileStream fs = new FileStream(saveFileDialog1.FileName + ".txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(richTextBox1.Text);
                sw.Close();
                fs.Close();

            }
            else if (DialogResult.No == MessageBox.Show(("Do you want to save changes to Untiteled"), "Notepad", MessageBoxButtons.YesNoCancel))
            {

                richTextBox1.Clear();

            }
            else if (DialogResult.Cancel == MessageBox.Show(("Do you want to save changes to Untiteled"), "Notepad", MessageBoxButtons.YesNoCancel))
            {


                 ***//when i click on cancel button...the dialogbox should be close??????????????????????***
            }

        }
        else
        { 
            richTextBox1.Clear(); 
        }

    }
+2

I don't know if C # has the same behavior, but in Java I change the constructor of the message box and pass a link to the sender form.

MBox1 = New MBox(ParentForm sender);

Then in the message box you can:

sender.close(); //or whatever
this.close();

The examples are more "pseudo-code-like", but I hope this helps

0
source

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


All Articles