计算使用的文本框数量,并使用该值结束循环

时间:2013-07-02 13:33:59

标签: c#

我是c#的新手,我想知道如何计算textBox的总数以结束循环。因此,例如:如果有8个文本框,并且用户在8个中的5个中输入值,我想知道如何计算使用的5并结束循环,并在说明使用了多少个文本框后显示messageBox

//这是代码

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

4 个答案:

答案 0 :(得分:2)

您可以使用扩展方法获取表单中的文本框。

public static IEnumerable<TControl> GetChildControls(this Control control) where TControl : Control
{
    var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>();
    return children.SelectMany(c => GetChildControls(c)).Concat(children);
}

获取所有文本框。

var allTextBoxes = this.GetChildControls<TextBox>();

检查每个文本框中的值。

int sum = 0;
foreach (TextBox oTextBox in this.GetChildControls<TextBox>())
{
    if(oTextBox.Text != string.Empty)
       sum += 1;
}

参考:https://stackoverflow.com/a/4863138/1537422

答案 1 :(得分:1)

你应该使用this.Controls

  int sum = 0;
  foreach (TextBox tb in this.Controls.OfType<TextBox>())
  {
    if(tb.Text != string.Empty) //textbox is not empty do something
        sum += int.Parse(tb.Text);
    else break; // here you can detect if textbox is empty
  }

您可以在Form的Load事件中使用此代码。上面的示例判断非空文本框的总和并打破第一个空文本框上的循环。你可以修改它不是为了打破循环而只是不计算空的文本框。

要以递归方式查找TextBox,请检查this link

答案 2 :(得分:1)

int count = 0; // create counter
foreach (Control c in Controls) // start loop through all the controls in the current form
{
    if(!(c is TextBox)) continue; // if the control is not a TextBox, continue the loop
    TextBox t = c as TextBox; // redeclare
    if(t.Text != "") // if the text in the textbox is NOT empty
    {
        count++; // add to the counter
    }
} // end loop
if (count >= 5) // check if the counter is higher or equal than 5
{
    // do stuff!
}

我希望这会帮助你。我还没有自己运行它,但它应该工作。 (我希望!)

答案 3 :(得分:0)

您可以使用

int cnt = 0;

foreach (Control c in this.Controls)
    if (c is System.Windows.Forms.TextBox)
        if (!string.IsNullOrEmpty(c.Text))
            cnt++;