Quote Originally Posted by VB.NET Developer View Post
OK, here´s complete code:

Code:
If ActualWord.Text.Contains("_") Then
ActualWord.Text = ""
TextBox_Words.Text = TextBox_Words.Text.Remove(TextBox_Words.Text.LastIndexOf(Environment.NewLine))
TextBox_Words.Text = TextBox_Words.Text.Remove(TextBox_Words.Text.LastIndexOf(Environment.NewLine))
Dim lines() = TextBox_Words.Text.Split(Chr(10))
Dim lastLine = lines.Last()
ProcessedWords.Text += lastLine + vbNewLine
TextBox_Words.Clear()
Timer1.Stop()
GC.Collect()
Given that the LF is the second character in a CR-LF pair, splitting on just the LF will actually give you the correct value for the last line, while all lines before that will have a rogue CR at the end. That being the case, if the specific point was to get the last line then it would work. That's probably a bit of a bad habit to get into though and, if what you want is the text after the last LF in a String then LastIndexOf and Substring would be more efficient.

That code is really rather dodgy though. It appears that what you actually want is the third last line from the TextBox but you are just making it the last with some bad code. This:
vb.net Code:
  1. TextBox_Words.Text = TextBox_Words.Text.Remove(TextBox_Words.Text.LastIndexOf(Environment.NewLine))
  2. TextBox_Words.Text = TextBox_Words.Text.Remove(TextBox_Words.Text.LastIndexOf(Environment.NewLine))
  3. Dim lines() = TextBox_Words.Text.Split(Chr(10))
  4. Dim lastLine = lines.Last()
  5. ProcessedWords.Text += lastLine + vbNewLine
ought to be replaced with this:
vb.net Code:
  1. Dim lines = TextBox_Words.Lines
  2.  
  3. ProcessedWords.AppendText(lines(lines.Length - 3) & Environment.NewLine)
There's no point removing two lines from the TextBox just to make the line you want the last one when you can get it without it being the last very easily anyway. Also, use AppendText to append text rather than setting the Text property.