How to loop the dialogresult
-
09-03-2021 - |
Вопрос
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();
Не связан с StackOverflow