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);
}
}
}
答案 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;
}