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.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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);
}
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.
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
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);
}
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.