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
textBox_SourceCount.Text = itemCount.ToString();
Also, you don't need to use the LINQ Count function as a List has a Count property.
var itemCount = sortBox.Count(); // Calls a LINQ function which calls the Count property
var itemCount = sortBox.Count; // Calls the Count property directly
For future reference, C# will not automatically cast an int
to a string
. You need to perform the conversion explicitly in most cases.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.