I creating multiline TextBox
with this Link its work better but if I want to set TextBox
text counter
label1.Content = textBox1.Text.Length;
with above line work fine but problem is that when I press enter in the TextBox
counter it will increase 2 characters in TextBox
counter.
How can I do this task please help me.
Any help appreciated!
Andrey Gordeev's answer is right (+1 for him) but does not provide a direct solution for your problem. If you check the textBox1.Text
string with the debugger you would see the referred \r\n
characters. On the other hand, if you intend to affect them directly (via .Replace
, for example), you wouldn't get anything.
Thus, the practical answer to your question is: rely on Environment.NewLine
. Sample code:
label1.Content = textBox1.Text.Replace(Environment.NewLine, "").Length;
That's because newline is presented by two symbols: \r
and \n
Related question: What is the difference between \r and \n?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With