views:

1611

answers:

3

Hello. Is there a graceful way in C# to delete multiple lines of text from the beginning of a multiline textbox? I am using Microsoft Visual C# 2008 Express Edition. Thanks.

EDIT - Additional Details The multiline textbox in my application is disabled (i.e. it is only editable by the application itself), and every line is terminated with a "\r\n".

+1  A: 

Unfortunately, no, there is no "elegant" way to delete lines from the text of a multiline TextBox, regardless of whether you are using ASP.NET, WinForms, or WPF/Silverlight. In every case, you build a string that does not contain the lines you don't want and set the Text property.

WinForms will help you a little bit by pre-splitting the Text value into lines, using the Lines property, but it's not very helpful because it's a string array, and it's not exactly easy to delete an element of an array.

Generally, this algorithm will work for all possible versions of the TextBox class:

var lines = (from item in myTextBox.Text.Split('\n') select item.Trim());
lines = lines.Skip(numLinesToSkip);
myTextBox.Text = string.Join(Environment.Newline, lines.ToArray());

Note: I'm using Environment.Newline specifically for the case of Silverlight on a Unix platform. For all other cases, you're perfectly fine using "\r\n" in the string.Join call.

Also, I do not consider this an elegant solution, even though it's only 3 lines. What it does is the following:

  • splits the single string into an array of strings
  • iterates over that array and builds a second array that does not include the lines skipped
  • joins the array back into a single string.

I do not consider it elegant because it essentially builds two separate arrays, then builds a string from the second array. A more elegant solution would not do this.

Randolpho
+1 for a variation that works on all TextBox classes.
Stan R.
Thanks a bunch, Stan!
Randolpho
+4  A: 

This is an incomplete question. So assuming you are using either TextBox or RichTextBox you can use the Lines property found inTextBoxBase.

//get all the lines out as an arry
string[] lines = this.textBox.Lines;

You can then work with this array and set it back.

  this.textBox.Lines= newLinesArray;

This might not be the most elegant way, but it will remove the first line. EDIT: you don't need select, just using skip will be fine

    //number of lines to remove from the beginning
    int numOfLines = 30; 
    var lines = this.textBox1.Lines;
    var newLines = lines.Skip(numOfLines);

    this.textBox1.Lines = newLines.ToArray();
Stan R.
NOTE: WPF's TextBox does not contain a Lines property. This will only work for WinForms.
Randolpho
Thanks, Stan, but I don't understand the "line => line" part of your example. I tried lines.Skip(30).Select(31, lines.Count()) but it errored.
Jim Fell
@Jim you only need Skip(30).Select( line => line). Skip means to skip 30 lines and Select is usually a transformation, so in this case there is no transformation we are just selecting the remaining lines after. In fact the .Select is redundant and we don't even need it. I updated my answer.
Stan R.
+1  A: 

One thing to keep in mind is that the Lines collection of the TextBox does not accurately reflect what the user sees as lines. The Lines collection basically works off of carriage returns, whereas the user could see lines wrapping from one line to the next without a carriage return. This may or may not be the behavior you want.

For example, the user would see the below as three lines, but the Lines collection will show 2 (since there are only 2 carriage returns):

This is line number 
one. 
This is line 2.

Also, if the form, and the text control are resizable the visible lines in the text will change as the control grows or shrinks.

I wrote a blog post several years ago on how to determine the number of lines in the textbox as the user sees them and get the index of a given line (like to get the line at index: http://ryanfarley.com/blog/archive/2004/04/07/511.aspx, perhaps this post will help.

Ryan Farley
this is a valid point.
Stan R.
The multiline textbox in my application is disabled (i.e. it is only editable by the application itself), and every line is terminated with a "\r\n". (I've added this comment to my initial question.) Thanks for the heads up.
Jim Fell