如何显示消息框只一次
本文关键字:消息 只一次 显示 何显示 | 更新日期: 2023-09-27 18:36:38
>在这里学习 c# 的新手,我计算 cgpa,当用户选择他们参加的主题数量时,相应地,文本框将根据用户主题的数量启用 true,其余的启用为 false。因此,当我单击计算CGPA时,如果用户输入为空,我想弹出消息,但根据用户留空的数字显示x时间。如何让它只显示一次。高级 Tqvm。非常感谢解释。
1.检查用户选中单选按钮
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.显示消息框何时点击计算
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);
}
}
}
创建一个显示带有全局标志的消息框的方法:
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")
您应该有以下行:
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);
}
}
}
我不想
重新洗牌你的代码,最好的方法是在任何文本框为空时添加一个 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;
}