代码之家  ›  专栏  ›  技术社区  ›  ras bry

使用C查找所有空的文本框和组合框#

  •  0
  • ras bry  · 技术社区  · 2 年前

    大家好,我想以一种简单的方式找到所有空的TextBox和ComboBox。 如下图所示,如果字段为空,则显示文本框或组合框为空的消息和红色背景色。

    enter image description here

    我试过了,但出了问题,感谢那些能解决这个问题的人。

            TextBox [] textBox = { textBox1, textBox2};
            ComboBox[] comboBox = { comboBox1, comboBox2 };
    
            foreach (TextBox txt in textBox)
            {
                if (string.IsNullOrEmpty(txt.Text))//if all textbox is empty
                {
                    txt.BackColor = Color.Red;
                    string message = "Required field empty.";
                    string tittle = "Error";
                    MessageBoxButtons button = MessageBoxButtons.OK;
                    DialogResult result = MessageBox.Show(message, tittle, button, MessageBoxIcon.Error);
                    break;
    
                }
                else if (!string.IsNullOrEmpty(txt.Text))//if not empty
                {
    
                    txt.BackColor = Color.White;
                    MessageBox.Show("task complete");
                    break;
                }
    
            }
            return;
    
    2 回复  |  直到 2 年前
        1
  •  1
  •   Suryateja KONDLA    2 年前
    // Arrays to hold your TextBox and ComboBox controls
    TextBox[] textBoxes = { textBox1, textBox2 };
    ComboBox[] comboBoxes = { comboBox1, comboBox2 };
    
    bool isEmpty = false; // Flag to indicate if any field is empty
    
    // Check each TextBox
    foreach (TextBox txt in textBoxes)
    {
        if (string.IsNullOrEmpty(txt.Text))
        {
            txt.BackColor = Color.Red;
            isEmpty = true;
        }
        else
        {
            txt.BackColor = Color.White;
        }
    }
    
    // Check each ComboBox
    foreach (ComboBox cmb in comboBoxes)
    {
        if (string.IsNullOrEmpty(cmb.Text))
        {
            cmb.BackColor = Color.Red;
            isEmpty = true;
        }
        else
        {
            cmb.BackColor = Color.White;
        }
    }
    
    // Show a single message if any field is empty
    if (isEmpty)
    {
        string message = "Required field empty.";
        string title = "Error";
        MessageBoxButtons buttons = MessageBoxButtons.OK;
        MessageBox.Show(message, title, buttons, MessageBoxIcon.Error);
    }
    else
    {
        MessageBox.Show("Task complete");
    }
    
        2
  •  1
  •   Auditive    2 年前

    您可以通过选择表单的所有控件 this.Controls ,然后使用LINQ进行过滤 Where 只取 TextBox ComboBox 控件,以及 .Text 属性为空。

    using System;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;
    // ...
    
    private void ButtonSubmit_Click(object sender, EventArgs e)
    {
        // Find and take from all controls of the form only TextBox'es and ComboBox'es, which .Text is empty
        var unfilledControls = this.Controls.Cast<Control>()
                               .Where(c => c is TextBox or ComboBox && string.IsNullOrEmpty(c.Text))
                               .ToList();
    
        // Check if there was found unfilled controls
        if (unfilledControls.Count == 0)
        {
            // Here you can proceed submit
            // DoSubmit();
            // MessageBox.Show("Submitted!");
            // this.Close(); // to close form, or...
            return;
        }
         
        // Here something is unfilled, so apply "warning" to control(s) or notify user
           
        unfilledControls.ForEach(c => c.BackColor = Color.Red);
        MessageBox.Show("Some required fields are empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    

    看起来像:

    您还可以通过设置“警告”行为 TextChanged 属性,例如,在形式构造函数中。只需将匿名委托应用于该事件。所以它会改变 BackColor 并“动态”检查用户输入。

    public Form3()
    {
        InitializeComponent();
    
        this.Controls.Cast<Control>()
        .Where(c => c is TextBox or ComboBox)
        .ToList()
        .ForEach(control =>
        {
             control.TextChanged += delegate
             {
                 control.BackColor = string.IsNullOrEmpty(control.Text)
                    ? Color.Red
                    : Color.White;
             };
        });
    }