Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline issue WPF TextBox

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!

like image 817
Jay Shukla Avatar asked Aug 27 '13 07:08

Jay Shukla


2 Answers

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;
like image 181
varocarbas Avatar answered Oct 02 '22 21:10

varocarbas


That's because newline is presented by two symbols: \r and \n

Related question: What is the difference between \r and \n?

like image 22
Andrey Gordeev Avatar answered Oct 02 '22 23:10

Andrey Gordeev