如何只显示一次messageBox?

时间:2016-02-28 05:12:36

标签: c#

在这里学习c#的新手,我正在计算cgpa,当用户选择他们拍摄的主题数时,相应地,textBox将根据用户主题的数量启用为true,其余为Enabled false。因此当我点击calculateCGPA时,如果用户输入为空,我想弹出消息,但是根据用户留空的数字显示消息框x时间。如何让它只显示一次。 Tqvm在先进。非常感谢解释。

1.CheckingUserCheckedRadioButton

private void DisplayTextBox(Control con)
    {
        foreach (Control c in con.Controls)
        {
            if (rad1.Checked)
            {
                if (c is TextBox)
                {
                    ((TextBox)c).Enabled = false;
                    txtCCode1.Enabled = true;
                    txtGrade1.Enabled = true;
                }
                else
                {
                    DisplayTextBox(c);
                }

            }
       }
}

2.DisplayingMessageBoxWhenClickingCalculate

private void calculate(Control con)
    {
        foreach (Control c in con.Controls)
        {
            if (c is TextBox)
            {
                if (c.Text == "")
                {
                    DialogResult x = new DialogResult();
                    x = MessageBox.Show("TextBox cannot be Empty");
                    if (x == DialogResult.OK)
                        txtCCode1.Focus();
                }
                else
                {
                    int totalCredHours = 0;
                    CalcTotalCredHours(credHour1, credHour2, credHour3, credHour4, credHour5, credHour6, ref totalCredHours);
                    courseGP1 = CalcCourseGradePoint(credHour1, gradePoint1);
                    courseGP2 = CalcCourseGradePoint(credHour2, gradePoint2);
                    courseGP3 = CalcCourseGradePoint(credHour3, gradePoint3);
                    courseGP4 = CalcCourseGradePoint(credHour4, gradePoint4);
                    courseGP5 = CalcCourseGradePoint(credHour5, gradePoint5);
                    courseGP6 = CalcCourseGradePoint(credHour6, gradePoint6);
                    double totalCGP = CalcTotalCGP(courseGP1, courseGP2, courseGP3, courseGP4, courseGP5, courseGP6);
                    double gpa = CalcGPA(totalCGP, totalCredHours);
                    lblGPA.Text = gpa.ToString("N");
                }
            }
            else
            {
                calculate(c);
            }
        }              
    }

3 个答案:

答案 0 :(得分:1)

创建一个显示带有全局标志的消息框的方法:

bool showed = false;
private ShowMessageBox(string message)
{
    if (!showed)
         MessageBox.Show(message);
         showed = true;
}

在您的代码中调用此方法

ShowMessageBox("TextBox cannot be Empty") 

而不是

MessageBox.Shows("TextBox cannot be Empty")

答案 1 :(得分:1)

你应该有以下几行:

static bool showed = false;                                 // <---- This line

private void DisplayTextBox(Control con)
{
    if (rad1.Checked)
    {
        foreach (Control c in con.Controls)
        {
            if (c is TextBox)
            {
                ((TextBox)c).Enabled = false;
                txtCCode1.Enabled = true;
                txtGrade1.Enabled = true;
            }
            else
            {
                DisplayTextBox(c);
            }

        }
    }
    showed = false;                                         // <---- This line
}

private void calculate(Control con)
{
    foreach (Control c in con.Controls)
    {
        if (c is TextBox)
        {
            if (c.Text == "")
            {
                if (!showed)                                // <---- This line
                {                                           // <---- This line
                    showed = true;                          // <---- This line
                    DialogResult x = new DialogResult();
                    x = MessageBox.Show("TextBox cannot be Empty");
                    if (x == DialogResult.OK)
                        txtCCode1.Focus();

                }                                           // <---- This line
            }
            else
            {
                int totalCredHours = 0;
                CalcTotalCredHours(credHour1, credHour2, credHour3, credHour4, credHour5, credHour6, ref totalCredHours);
                courseGP1 = CalcCourseGradePoint(credHour1, gradePoint1);
                courseGP2 = CalcCourseGradePoint(credHour2, gradePoint2);
                courseGP3 = CalcCourseGradePoint(credHour3, gradePoint3);
                courseGP4 = CalcCourseGradePoint(credHour4, gradePoint4);
                courseGP5 = CalcCourseGradePoint(credHour5, gradePoint5);
                courseGP6 = CalcCourseGradePoint(credHour6, gradePoint6);
                double totalCGP = CalcTotalCGP(courseGP1, courseGP2, courseGP3, courseGP4, courseGP5, courseGP6);
                double gpa = CalcGPA(totalCGP, totalCredHours);
                lblGPA.Text = gpa.ToString("N");
            }
        }
        else
        {
            calculate(c);
        }
    }              
}

答案 2 :(得分:0)

我不想重新编写代码,最好的方法是在任何文本框为空时添加 break 语句。

For e.g
foreach (Control c in con.Controls)
        {
            if (c is TextBox)
            {
                if (c.Text == "")
                {
                    DialogResult x = new DialogResult();
                    x = MessageBox.Show("TextBox cannot be Empty");
                    if (x == DialogResult.OK)
                        txtCCode1.Focus();
                        break;
                }