1

我是 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
4

4 回答 4

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

于 2013-07-02T13:47:16.480 回答
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!
}

我希望这能够帮到你。我自己还没有运行它,但它应该可以工作。(我希望!)

于 2013-07-02T13:46:11.707 回答
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,请检查此链接

于 2013-07-02T13:39:00.133 回答
0

您可以使用

int cnt = 0;

foreach (Control c in this.Controls)
    if (c is System.Windows.Forms.TextBox)
        if (!string.IsNullOrEmpty(c.Text))
            cnt++;
于 2013-07-02T13:47:13.287 回答