0

I'm having a problem in keeping the Parent Form to be opened till I close after using ShowDialog() .

I've been trying regarding this but couldn't get. I think there is something simple that I might have been missing. Can you please help me reg this?

The problem is, I have Form 1, on pressing one button, Form 2 opens. I do some validations in Form 2, and check for the validations. If the validation doesn't pass, I open a DialogBox form, with Retry and Cancel. If I press Retry, The control should go back to the Form 2 and form 2 should not close. If the press Cancel, both the DialogBox form and the Form 2 should close. Right now, regardless of what I press, both the forms close.

I have looked online and couldn't find any solution. Went through this solution, but both the forms are still closing for me. Why does closing a nested child dialog also close the parent dialog?

My code:(Sample example scenario)

Form 1:

private void button1_Click(object sender, EventArgs e)
{
    Form2 testForm = new Form2();
    DialogResult dialogResult = new DialogResult();
    dialogResult = testForm.ShowDialog(this);
    if(dialogResult == DialogResult.OK)
    {
        //Do something
    }
}

Form 2:

private void button1_Click(object sender, EventArgs e)
{

    DialogResult validDataResult = MessageBox.Show("Invalid Data Entered. Please provide the correct data."
            , "Data Management"
            , MessageBoxButtons.RetryCancel);

    if (validDataResult == DialogResult.Cancel)
    {
        this.Close();
    }
}
Community
  • 1
  • 1
Sailoosha
  • 193
  • 1
  • 3
  • 14
  • With Above code its not reproducable. Are you inheriting Form2 from Form1 and writing your own virtual Close Method in Form1 ? – Dhananjay Apr 11 '12 at 04:52

2 Answers2

1

in Form2.cs do your validation and then
(assuming validationOK is the true/false result of your checks)

if(validationOK == false)
{
    // Ask retry or cancel to the user
    if(DialogResult.Cancel == MessageBox.Show("Validation Fail", "Validation failed, press retry to do it againg", MessageBoxButtons.RetryCancel))
        this.DialogResult.Cancel; // Set the dialog result on form2. This will close the form.

    // if you have the validation done in a button_click event and that button has its
    // property DialogResult set to something different than DialogResult.None, we need
    // to block the form2 from closing itself.

    // uncomment this code if the above comment is true
    // else
    //    this.DialogResult = DialogResult.None;
}
Steve
  • 213,761
  • 22
  • 232
  • 286
0

You have to set the DialogResult of Form2 before you can call this.Close(). Otherwise, it remains the default. (The below code is only a guess of the actual double close logic since you did not specify that)

Form2.cs:

if (validDataResult == DialogResult.Cancel)
    DialogResult = DialogResult.Cancel;
else
    DialogResult = DialogResult.OK;
Close();

Form1.cs:

if(dialogResult == DialogResult.OK)
{
    Close();
}
else
{
    //Do something
}
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • Hi Justin. I don't want the Form 1 to be closed anytime. I have set the DialogResult in form 2 before closing. But, I need not close the Form 2 explicitly. dialogResult = testForm.ShowDialog(this);(From Form1.cs). Calling the ShowDialog is automatically executing the button1 click event and is closing the form. Any suggestions about how I can change that? – Sailoosha Apr 09 '12 at 17:39
  • 1
    @Sailoosha I think there is a miscommunication here. Can you post your entire Form1 and Form2.cs code? I think you are leaving out a key piece of data. At bare minimum, when you say it is executing the click event, is it closing the form before it opens, or is it popping up the Invalid Data entered dialog? – Justin Pihony Apr 09 '12 at 18:07
  • Thanks Justin. I just got the solution. I did little modification to the code you suggested. Changing the DialogResult of the form to NONE solved the issue. – Sailoosha Apr 09 '12 at 18:26
  • I'm not able to post the entire code here. I'll be able to post after few hours under the answer section. – Sailoosha Apr 09 '12 at 18:34