question

WilfredEghenedji-9495 avatar image
0 Votes"
WilfredEghenedji-9495 asked $$ANON_USER$$ commented

How to count textboxes having values from specific list of textboxes

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);
     } 

dotnet-csharpwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Try this minor change too:

 int countTB = form1.Controls.OfType<TextBox>( ).Count( tb => ! string.IsNullOrWhiteSpace( tb.Text ));


1 Vote 1 ·
$$ANON_USER$$ avatar image
1 Vote"
$$ANON_USER$$ answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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);
     }





figure1.png (7.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WilfredEghenedji-9495 avatar image
0 Votes"
WilfredEghenedji-9495 answered $$ANON_USER$$ commented

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.





· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You are welcome.

0 Votes 0 ·