-2
Form1 frm = new Form1();
frm1.ShowDialog();

I use this to create new form to do some stuff(not important), and then after i'm done with the form i display dialogresult to open that same form again. Question: how can i continue to open the same form? While the Dialogresult is YES keep the form open(how to loop this)? the NO property breaks the loop. I hope the question is clear.

Michael27
  • 161
  • 1
  • 13

2 Answers2

2

Here is my suggestion for you:

var form2 = new Form2();

do
{
    form2.ShowDialog();
}
while (form2.DialogResult == System.Windows.Forms.DialogResult.Yes);

EDIT:

I take from your comment to your question, that you want to use a MessageBox, so you can go like that:

var form2 = new Form2();

do
{
    form2.ShowDialog();
}
while (MessageBox.Show(string.Format("The DialogResult was {0}, do you want to show the form again?", form2.DialogResult), "My Program", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes);
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
  • 2
    As far as I understood your question, you want to loop as long as the DialogResult of a called form is Yes. So my code shows the form and if the DialogResult is Yes, it loops and shows the form again. If there is anything other than Yes, the loop stops. – Fischermaen Nov 26 '11 at 18:45
  • I changed the sample according to the comment to your question. – Fischermaen Nov 26 '11 at 19:23
1

If when the user tries to close frm1 you are displaying a dialogresult, just don't do anything if the answer is yes. Otherwise close frm1. I hope I understood your question correctly.

Edit: When you want to close frm1:

DialogResult res = MessageBox.Show("Do you want to keep this form open?", 
                                   "Close?",
                                   MessageBoxButtons.YesNo);

if(res == DialogResult.No) this.Close();
Tudor
  • 61,523
  • 12
  • 102
  • 142
  • I tried to use switch case but it only works once, the dialog is displayed only once, no matter what i do. – Michael27 Nov 26 '11 at 18:36