我是 c# 新手,我想知道如何计算文本框的总数以结束循环。因此,例如:如果有 8 个文本框并且用户在 8 个中的 5 个中输入了值,我想知道如何计算使用的 5 个并结束循环,并在说明使用了多少个文本框后显示一个消息框.
// 这里是代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GPA_Calculator
{
public partial class FormTitle : Form
{
public FormTitle()
{
InitializeComponent();
}
private void Calculatebtn_Click(object sender, EventArgs e)
{
// Declare variables
double grade = 0.0;
double total_grade = 0.0;
double pre_gpa = 0.0;
double gpa_result = 0.0;
int class_one_textBox = 0;
int class_two_textBox = 0;
int class_three_textBox = 0;
int class_four_textBox = 0;
int class_five_textBox = 0;
#region // Checks if all fields are empty
if ((textBox1.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox1.SelectedText))
&& (textBox2.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox2.SelectedText))
&& (textBox3.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox3.SelectedText))
&& (textBox4.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox4.SelectedText))
&& (textBox5.Text.Trim() == string.Empty) && (string.IsNullOrEmpty(comboBox5.SelectedText)))
{
MessageBox.Show("Empty Fields!"); // Display message to user if field is empty
}// end if
#endregion
**/*NEED A BETTER LOOP HERE*/**
for (double i = 0; i <= total_grade.ToString().Count(); i++)
{
#region // Class 1 Calculations
if ((comboBox1.SelectedIndex > -1) && (textBox1.Text.Trim() == string.Empty)
|| (comboBox1.SelectedIndex == -1) && (textBox1.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 1 contains an empty field!");
}
else if ((comboBox1.SelectedIndex > -1) && (textBox1.Text.Trim() != string.Empty))
{
switch (comboBox1.SelectedItem.ToString())
{
case "A":
class_one_textBox = int.Parse(textBox1.Text);
grade = 4.00 * class_one_textBox;
break;
case "A-":
class_one_textBox = int.Parse(textBox1.Text);
grade = 3.67 * class_one_textBox;
break;
case "B+":
class_one_textBox = int.Parse(textBox1.Text);
grade = 3.33 * class_one_textBox;
break;
case "B":
class_one_textBox = int.Parse(textBox1.Text);
grade = 3.00 * class_one_textBox;
break;
case "B-":
class_one_textBox = int.Parse(textBox1.Text);
grade = 2.67 * class_one_textBox;
break;
case "C+":
class_one_textBox = int.Parse(textBox1.Text);
grade = 2.33 * class_one_textBox;
break;
case "C":
class_one_textBox = int.Parse(textBox1.Text);
grade = 2.00 * class_one_textBox;
break;
case "C-":
class_one_textBox = int.Parse(textBox1.Text);
grade = 1.67 * class_one_textBox;
break;
case "D":
class_one_textBox = int.Parse(textBox1.Text);
grade = 1.33 * class_one_textBox;
break;
case "F":
class_one_textBox = int.Parse(textBox1.Text);
grade = 1.00 * class_one_textBox;
break;
} // end switch
} // end if
#endregion
#region // Class 2 Calculations
if ((comboBox2.SelectedIndex > -1) && (textBox2.Text.Trim() == string.Empty)
|| (comboBox2.SelectedIndex == -1) && (textBox2.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 2 contains an empty field!");
}
else if ((comboBox2.SelectedIndex > -1) && (textBox2.Text.Trim() != string.Empty))
{
switch (comboBox2.SelectedItem.ToString())
{
case "A":
class_two_textBox = int.Parse(textBox2.Text);
grade = 4.00 * class_two_textBox;
break;
case "A-":
class_two_textBox = int.Parse(textBox2.Text);
grade = 3.67 * class_two_textBox;
break;
case "B+":
class_two_textBox = int.Parse(textBox2.Text);
grade = 3.33 * class_two_textBox;
break;
case "B":
class_two_textBox = int.Parse(textBox2.Text);
grade = 3.00 * class_two_textBox;
break;
case "B-":
class_two_textBox = int.Parse(textBox2.Text);
grade = 2.67 * class_two_textBox;
break;
case "C+":
class_two_textBox = int.Parse(textBox2.Text);
grade = 2.33 * class_two_textBox;
break;
case "C":
class_two_textBox = int.Parse(textBox2.Text);
grade = 2.00 * class_two_textBox;
break;
case "C-":
class_two_textBox = int.Parse(textBox2.Text);
grade = 1.67 * class_two_textBox;
break;
case "D":
class_two_textBox = int.Parse(textBox2.Text);
grade = 1.33 * class_two_textBox;
break;
case "F":
class_two_textBox = int.Parse(textBox2.Text);
grade = 1.00 * class_two_textBox;
break;
} // end switch
} // end if
#endregion
#region // Class 3 Calculations
if ((comboBox3.SelectedIndex > -1) && (textBox3.Text.Trim() == string.Empty)
|| (comboBox3.SelectedIndex == -1) && (textBox3.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 3 contains an empty field!");
}
else if ((comboBox3.SelectedIndex > -1) && (textBox3.Text.Trim() != string.Empty))
{
switch (comboBox3.SelectedItem.ToString())
{
case "A":
class_three_textBox = int.Parse(textBox3.Text);
grade = 4.00 * class_three_textBox;
break;
case "A-":
class_three_textBox = int.Parse(textBox3.Text);
grade = 3.67 * class_three_textBox;
break;
case "B+":
class_three_textBox = int.Parse(textBox3.Text);
grade = 3.33 * class_three_textBox;
break;
case "B":
class_three_textBox = int.Parse(textBox3.Text);
grade = 3.00 * class_three_textBox;
break;
case "B-":
class_three_textBox = int.Parse(textBox3.Text);
grade = 2.67 * class_three_textBox;
break;
case "C+":
class_three_textBox = int.Parse(textBox3.Text);
grade = 2.33 * class_three_textBox;
break;
case "C":
class_three_textBox = int.Parse(textBox3.Text);
grade = 2.00 * class_three_textBox;
break;
case "C-":
class_three_textBox = int.Parse(textBox3.Text);
grade = 1.67 * class_three_textBox;
break;
case "D":
class_three_textBox = int.Parse(textBox3.Text);
grade = 1.33 * class_three_textBox;
break;
case "F":
class_three_textBox = int.Parse(textBox3.Text);
grade = 1.00 * class_three_textBox;
break;
} // end switch
} // end else/if
#endregion
#region // Class 4 Calculations
if ((comboBox4.SelectedIndex > -1) && (textBox4.Text.Trim() == string.Empty)
|| (comboBox4.SelectedIndex == -1) && (textBox4.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 4 contains an empty field!");
}
else if ((comboBox4.SelectedIndex > -1) && (textBox4.Text.Trim() != string.Empty))
{
switch (comboBox4.SelectedItem.ToString())
{
case "A":
class_four_textBox = int.Parse(textBox4.Text);
grade = 4.00 * class_four_textBox;
break;
case "A-":
class_four_textBox = int.Parse(textBox4.Text);
grade = 3.67 * class_four_textBox;
break;
case "B+":
class_four_textBox = int.Parse(textBox4.Text);
grade = 3.33 * class_four_textBox;
break;
case "B":
class_four_textBox = int.Parse(textBox4.Text);
grade = 3.00 * class_four_textBox;
break;
case "B-":
class_four_textBox = int.Parse(textBox4.Text);
grade = 2.67 * class_four_textBox;
break;
case "C+":
class_four_textBox = int.Parse(textBox4.Text);
grade = 2.33 * class_four_textBox;
break;
case "C":
class_four_textBox = int.Parse(textBox4.Text);
grade = 2.00 * class_four_textBox;
break;
case "C-":
class_four_textBox = int.Parse(textBox4.Text);
grade = 1.67 * class_four_textBox;
break;
case "D":
class_four_textBox = int.Parse(textBox4.Text);
grade = 1.33 * class_four_textBox;
break;
case "F":
class_four_textBox = int.Parse(textBox4.Text);
grade = 1.00 * class_four_textBox;
break;
} // end switch
} // end else if
#endregion
#region // Class 5 Calculations
if ((comboBox5.SelectedIndex > -1) && (textBox5.Text.Trim() == string.Empty)
|| (comboBox5.SelectedIndex == -1) && (textBox5.Text.Trim() != string.Empty))
{
MessageBox.Show("Class 5 contains an empty field!");
}
else if ((comboBox5.SelectedIndex > -1) && (textBox5.Text.Trim() != string.Empty))
{
switch (comboBox5.SelectedItem.ToString())
{
case "A":
class_five_textBox = int.Parse(textBox5.Text);
grade = 4.00 * class_five_textBox;
break;
case "A-":
class_five_textBox = int.Parse(textBox5.Text);
grade = 3.67 * class_five_textBox;
break;
case "B+":
class_five_textBox = int.Parse(textBox5.Text);
grade = 3.33 * class_five_textBox;
break;
case "B":
class_five_textBox = int.Parse(textBox5.Text);
grade = 3.00 * class_five_textBox;
break;
case "B-":
class_five_textBox = int.Parse(textBox5.Text);
grade = 2.67 * class_five_textBox;
break;
case "C+":
class_five_textBox = int.Parse(textBox5.Text);
grade = 2.33 * class_five_textBox;
break;
case "C":
class_five_textBox = int.Parse(textBox5.Text);
grade = 2.00 * class_five_textBox;
break;
case "C-":
class_five_textBox = int.Parse(textBox5.Text);
grade = 1.67 * class_five_textBox;
break;
case "D":
class_five_textBox = int.Parse(textBox5.Text);
grade = 1.33 * class_five_textBox;
break;
case "F":
class_five_textBox = int.Parse(textBox5.Text);
grade = 1.00 * class_five_textBox;
break;
} // end switch
} // end else if
#endregion
total_grade = total_grade + grade;
}
// Sums up the amount of credits
pre_gpa = class_one_textBox + class_two_textBox + class_three_textBox
+ class_four_textBox + class_five_textBox;
gpa_result = total_grade / pre_gpa;
MessageBox.Show("Your GPA is " + gpa_result); // Displays GPA