How to count textboxes having values from specific list of textboxes

Wilfred Eghenedji 326 Reputation points
2021-06-05T00:06:49.393+00:00

I have 5 textboxes, i would like to obtain total count of all textboxe(s) that has value amongst the 5 textboxes. The total count appears in Label. The following code counts all textboxes in the form. I would appreciate if anyone can help edit it to do as aforementioned. Thank you.

protected void btnGetCount_Click(object sender, EventArgs e)
    {
        int countTB = 0;
        foreach (Control c in form1.Controls) //here is the minor change
        {
            if (c.GetType() == typeof(TextBox))
            {
                countTB++;
            }
        }

        Label2.Text=("No of TextBoxes: " + countTB);
    } 
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,927 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,401 questions
{count} votes

Accepted answer
  1. Anonymous
    2021-06-05T12:17:06.27+00:00

    Hello, I think you just need to change:

    if (c.GetType() == typeof(TextBox))
    

    To:

    if (c.GetType() == typeof(TextBox) && c.Text != "")
    

    I hope it helps.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,556 Reputation points
    2021-06-05T00:42:55.793+00:00

    The following code may go farther than you need. The Descendants method base method gets a specific type of control be it on the form or on a control such as a panel on a form. TextBoxList is a wrapper for Descendants. And one might say this is a lot of code but it's not really and I could had written a shorter version but this version is much better.

    using System.Collections.Generic;  
    using System.Linq;  
    using System.Windows.Forms;  
      
    namespace DescendantsDemo.Classes  
    {  
        public static class ControlExtensions  
        {  
            public static IEnumerable<T> Descendants<T>(this Control control) where T : class  
            {  
                foreach (Control child in control.Controls)  
                {  
                    T thisControl = child as T;  
                    if (thisControl != null)  
                    {  
                        yield return (T)thisControl;  
                    }  
      
                    if (child.HasChildren)  
                    {  
                        foreach (T descendant in Descendants<T>(child))  
                        {  
                            yield return descendant;  
                        }  
                    }  
                }  
            }  
      
            public static List<TextBox> TextBoxList(this Control control) => control.Descendants<TextBox>().ToList();  
        }  
    }  
    

    Form code

    using System;  
    using System.Linq;  
    using System.Windows.Forms;  
    using DescendantsDemo.Classes;  
      
    namespace DescendantsDemo  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void CountButton1_Click(object sender, EventArgs e)  
            {  
                label1.Text = $"Total: {this.TextBoxList().Count}";  
            }  
      
            private void CountButton2_Click(object sender, EventArgs e)  
            {  
                label1.Text = $@"Total: {this.TextBoxList().Where(textBox => textBox.Text == "Karen").Count()}";  
                  
            }  
        }  
    }  
    

    In the screenshot, left counts total TextBoxes while the right counts TextBoxes containing Karen
    102605-figure1.png

    Now for the simple code sample where all TextBoxes are directly on a form

    label1.Text = $"Total: {Controls.OfType<TextBox>().Count()}";  
    

    And

    label1.Text = $@"Total: {Controls.OfType<TextBox>().Where(textBox => textBox.Text == "Karen").Count()}";  
    

    To only work with specific TextBoxes

    public partial class Form1 : Form  
    {  
        private List<TextBox> _textBoxes = new List<TextBox>();  
        public Form1()  
        {  
            InitializeComponent();  
              
            Shown += OnShown;  
        }  
      
        private void OnShown(object sender, EventArgs e)  
        {  
            _textBoxes.Add(textBox1);  
            _textBoxes.Add(textBox2);  
        }  
    
    0 comments No comments

  2. Wilfred Eghenedji 326 Reputation points
    2021-06-06T07:35:08.76+00:00

    Using DarerenPeterson's answer, my original code was edited as:

         protected void btnGetCount_Click(object sender, EventArgs e)
         {
             int countTB = 0;
             foreach (Control c in form1.Controls)
             {
                      if (c.GetType() == typeof(TextBox) && c.Text != "") // here is the edited line
                 {
                     countTB++;
                 }
             }
    
             Label2.Text=("No of TextBoxes: " + countTB);
         } 
    

    However, Viorel-1's answer also worked too, when used with the code in the same line. karenpayneoregon's answer was great but had to serve me in another project. Thank you all for your kind attention.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.