是否可以显示最多5次的消息框?

问题描述 投票:-1回答:1

我希望我的while循环使用一个消息框最多提问5次。

当我运行它时,我会问一次,当我选择是时,会打开5个新表格。

如何解决此循环问题,以防止消息框询问用户是或不超过5次,以及如何选择一次,则防止打开5个表单?

我的代码

 private void btnNext_Click(object sender, EventArgs e)
    {
        int maxAddDriver = 5;
        int addDriver = 0;

        DialogResult answer = MessageBox.Show("Would you like to add an additional driver to policy?", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

        Hide();

        while (addDriver < maxAddDriver && answer == DialogResult.Yes)
        {
            addDriver++;

            Hide();

            new frmAdditionalDriver().Show();


            //answer = MessageBox.Show("Would you like to add an additional driver to policy?",
            //    "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
        }


        new frmInsurancePolicy().ShowDialog();

        Show();
    }
c# while-loop messagebox
1个回答
0
投票

请尝试以下操作:将maxAddDriver和addDriver移到按钮单击之外,这样就不会在每次单击按钮时重置它们,并保留以前的单击计数。

int maxAddDriver = 5;
int addDriver = 0;

private void btnNext_Click(object sender, EventArgs e)
{
    answer = MessageBox.Show("Would you like to add an additional driver to policy?", "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

    addDriver++;

    Hide();  


        //answer = MessageBox.Show("Would you like to add an additional driver to policy?",
        //    "Info", MessageBoxButtons.YesNo, MessageBoxIcon.Information);


    if (addDriver == maxAddDriver && answer == DialogResult.Yes)
    {
        new frmAdditionalDriver().Show();
    }
    else if (addDriver == maxAddDriver || answer == DialogResult.No)
    {
        new frmInsurancePolicy().ShowDialog();
    }

    Show();
}
© www.soinside.com 2019 - 2024. All rights reserved.