Getting the number of text lines in a TextBox

Related searches

I am trying to display the number of text lines in a textbox through the label.But, the thing is if the last line is empty, the label has to display the line numbers with out that empty line.

For example if they are 5 line with the last line as empty, then the label should display the number of lines as 4.


private void txt_CurrentVinFilter_EditValueChanged(object sender, EventArgs e)
   labelCurrentVinList.Text = string.Format("Current VIN List ({0})",  txt_CurrentVinFilter.Lines.Length.ToString());                       

Actually, above is the code...have to change to display only the no-empty lines in C# winforms.


You can also do this in a shorter way using LinQ. To count the lines and exlcude the last line if it is empty:

var lines = tb.Lines.Count();
lines -= String.IsNullOrWhiteSpace(tb.Lines.Last()) ? 1 : 0;

And to count only non-empty lines:

var lines = tb.Lines.Where(line => !String.IsNullOrWhiteSpace(line)).Count();

C# code to count number of lines in textbox ?, C# code to count number of lines in text box ? string strtext = textbox1.text; strtext = strtext.Replace('\r', '\n').Trim(); rslt = 0; foreach (string s in� How to: Get a Collection of Lines from a TextBox. 03/30/2017; 2 minutes to read +6; In this article. This example shows how to get a collection of lines of text from a TextBox. Example. The following example shows a simple method that takes a TextBox as the argument, and returns a StringCollection containing the lines of text in the TextBox.

This will not count any empty lines as the end

int count = tb.Lines.Length;
while (count > 0 && tb.Lines[count - 1] == "") {

Or, if you want to exclude also lines containing only whitespaces

int count = tb.Lines.Length;
while (count > 0 && tb.Lines[count - 1].Trim(' ','\t') == "" ) {

TextBox.LineCount Property (System.Windows.Controls), Gets the total number of lines in the text box. public: property int LineCount { int get(); };. C# Copy. In this video, we will learn how to get total number of lines in textbox in c#/csharp dotnet. how to read and write to text file in c#. get text in textbox in c# - Duration: 5:47.

If WordWrap is set to true and you want the number of lines that are displayed, try:

int count = textBox1.GetLineFromCharIndex(int.MaxValue) + 1;
// Now count is the number of lines that are displayed, if the textBox is empty count will be 1
// And to get the line number without the empty lines:
if (textBox1.Lines.Length == 0)
foreach (string line in textBox1.Lines)
    if (line == "")

How To Get Total Number of Lines in TextBox in C#, In this video, we will learn how to get total number of lines in textbox in C# Tutorial Duration: 1:05 Posted: 21 Jul 2016 In Excel, a textbox has a .LineCount property that will tell you how many lines are in the text. However, there is no property that will access specific lines within the text. Therefore, you will need to convert the .Text property of the textbox to an array of lines, and copy the excess lines to the other textbox using a For loop.

How to count the number of lines in multiline text box in C# 2.0 , The problem is that "lines" is a nebulous concept when you use a multiline textbox: because what the text box thinks of as a "line" is delimited� I have 2 text boxes in my form, one is for the content (mainTextBox), which is on the right, and the other is for the line numbers (linesTextBox), which is on the left. Both boxes are the same height and both are multi-line text boxes.

The simplest way is for us to call you at the number to verify it is yours. Alternatively, you can provide a copy of a phone bill that lists the number(s). To get started with a risk free trial, just call or text us at 850.205.1111 or complete the contact form.

The TextBox has a Lines property. int numLines = txt.Lines.Length But this only returns 1 during TextChanged event. Then you have just one line. Lines are separated by Ènvironment.NewLine (VBCrlf). Your text looks like it would have multiple lines but actually it is just wrapped since it's too long for the view.

  • Yeah, you do need to actually ask a question for people to answer.
  • If you like an answer, accept it as one