I have a multi-line TextBox that I can either type or paste items into.At the bottom of the textbox I have an “Item Count = ” label and “0” textbox next to it.
I would like the text in the “0” textbox to keep track of the number of items in my textbox list in real time. Is this possible?
This is what I have, but I can’t get it to work:
private void textBox2_TextChanged(object sender, EventArgs e)
{
char[] delimiterChars = { ',', ':', '|', '\n' };
List<string> sortBox =
new List<string>(textBox_ListSource.Text.Split(delimiterChars));
var itemCount = sortBox.Count();
textBox_SourceCount.Text = itemCount;
}
I am getting a red squiggly under the “itemCount” in the last line. It won’t compile and says can’t explicitly convert ‘int’ to ‘string’.
Try
Also, you don’t need to use the LINQ Count function as a List has a Count property.
For future reference, C# will not automatically cast an
intto astring. You need to perform the conversion explicitly in most cases.