Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to count lines in multi line TextBox for mobile.
Lines is not working with mobile.

Any idea will help

Thanks


Dear Rick Shaub
Thank you so much.
Big help to me.
It works fine.

This is for symbol scanner, so i have to -1.
This is what i used to count lines include your 2 lines of coding.
string[] lines = textBox1.Text.Split('\n');
int numberOflines = lines.Length -1;
textBox2.Text = Convert.ToString(numberOflines);
Posted
Updated 30-Dec-09 18:56pm
v2

Also, a more general solution would be to handle different types of newlines:
C#
int len = System.Text.RegularExpressions.Regex.Split("line1\r\nline2", @"\r?\n").Length;

Alternatively, you could use another version of string.Split:
C#
int len = "line1\r\nline2".Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None).Length;
 
Share this answer
 
v2
Can you post the code that's not working?

If you want the number of lines of text you could just use something like:

string[] lines = textbox1.Text.Split('\n');
int numberOflines = lines.Length;


Note: I'd just use Split('\n') because I've never seen '\r' use alone as a newline character. Additionally, Split('\n') will treat "\n" and "\r\n" identically.
 
Share this answer
 
v3
Comments
Charles Kuperus 24-Sep-10 12:49pm    
But what if the textbox have wordwrap on?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900