문제

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.

도움이 되었습니까?

해결책

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);

다른 팁

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top