有没有一种简单的方法可以确定所有文本框是否不为空,并在 C# Windows Form 中禁用或清除它们?

Is there an easy way to determine if all textbox are not empty and to disable or clear them in C# Windows Form?

我有一个项目,我需要在其中创建一个主订单 Windows 表单程序。

我有三个组框 Customer、Item 和 Summary。

我需要三个函数,其中:

• 1 个函数用于确定 Customer、Items 中的所有文本框是否都有输入 • 另一个禁用客户中所有文本框的功能 • 另一种清除客户、项目和摘要中所有文本框的功能。

我认为是为每个文本框创建 if 条件,但我认为还有另一种简单的方法可以做到这一点。

我的设计师选项卡:

我的Form.cs:

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 Mail_Order
{
public partial class Form1 : Form
{

    private static double totalAmountDue = 0, totalWeight = 0;

    private static double groundShipping;

    private static int items = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void label5_Click(object sender, EventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {

    }

    private void label8_Click(object sender, EventArgs e)
    {

    }

    private void textBox8_TextChanged(object sender, EventArgs e)
    {

    }

    private void label11_Click(object sender, EventArgs e)
    {

    }

    private void button4_Click(object sender, EventArgs e)
    {
        textAddrss.Clear();
        textCustName.Clear();
        textDescription.Clear();
        textPrice.Clear();
        textQuantity.Clear();
        textAmntDue.Clear();
        textCity.Clear();
        textSaleTax.Clear();
        textShipping.Clear();
        textState.Clear();
        textTotalAmntDue.Clear();
        textWeight.Clear();
        textZip.Clear();
        totalAmountDue = 0;
        totalWeight = 0;
    }

    private void groupBox1_Enter(object sender, EventArgs e)
    {

    }

    private void buttonAdd_Click(object sender, EventArgs e)
    {
        //validate input
        if (isNumeric(textQuantity.Text) == false || int.Parse(textQuantity.Text) <= 0)
            errorMessage();

        else if (isNumeric(textWeight.Text) == false || double.Parse(textWeight.Text) <= 0)
            errorMessage();

        else if (isNumeric(textPrice.Text) == false || double.Parse(textPrice.Text) <= 0)
            errorMessage();

        else
            totalAmountDue = totalAmountDue + (double.Parse(textPrice.Text) * double.Parse(textQuantity.Text));
    }

    private void buttonExit_Click(object sender, EventArgs e)
    {
        Close();
    }

    // identity if numeric
    private bool isNumeric(string s)
    {
        double input;
        return Double.TryParse(s, out input);
    }
    // diplay error for invalid input
    private void errorMessage()
    {
        MessageBox.Show("Invalid entry. Please enter a valid amount");
    }
}
}

谢谢大家!

感谢 Flydog57 和 AJITH。

我能够在线找到资源。

对于我使用的第一个函数:

// validate if all groupbox have input
    private void IsEmpty()
    {
        flag = false;
        
            foreach (Control cont in groupBox1.Controls)
            {
                if (cont is TextBox)
                {
                    if (((TextBox)cont).Text == "")
                         flag = true;
                }

            }

            foreach (Control cont in groupBox2.Controls)
            {
                if (cont is TextBox)
                {
                    if (((TextBox)cont).Text == "")
                         flag = true;
                }
            }
        
    }

第二

                    groupBox1.Enabled = false;

第三

private void button4_Click(object sender, EventArgs e)
    {
        //clear data

        foreach (Control cont in groupBox1.Controls)
        {
            if (cont is TextBox)
            {
                ((TextBox)cont).Text = " ";
            }
        }

        foreach (Control cont in groupBox2.Controls)
        {
            if (cont is TextBox)
            {
                ((TextBox)cont).Text = " ";
            }
        }

        foreach (Control cont in groupBox3.Controls)
        {
            if (cont is TextBox)
            {
                ((TextBox)cont).Text = " ";
            }
        }

        groupBox1.Enabled = true;

        totalAmountDue = 0; totalWeight = 0; tax = 0; dollarAmountDue = 0;

        groundShipping = 0; shipping = 0; handling = 0;
}

您可以通过一些重构来减少代码并使其更清晰:

private bool flag;
private GroupBox[] groups;

private void Form1_Load(object sender, EventArgs e)
{
    groups = new GroupBox[] { groupBox1, groupBox2, groupBox3 };
}        

// validate if all groupbox have input
private void IsEmpty()
{
    flag = false;            
    for(int i=0; i<groups.Length && !flag; i++)
    {
        foreach(TextBox tb in groups[i].Controls.OfType<TextBox>())
        {
            if (tb.Text.Trim().Length == 0)
            {
                flag = true;
                break;
            }
        }
    }
}

private void button4_Click(object sender, EventArgs e)
{
    //clear data
    foreach(GroupBox gb in groups)
    {
        foreach(TextBox tb in gb.Controls.OfType<TextBox>())
        {
            tb.Text = " ";
        }
    }
    groupBox1.Enabled = true;
    totalAmountDue = 0; totalWeight = 0; tax = 0; dollarAmountDue = 0;
    groundShipping = 0; shipping = 0; handling = 0;
}