Results 1 to 12 of 12

Thread: [RESOLVED] Copy the last line from textbox into another textbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Resolved [RESOLVED] Copy the last line from textbox into another textbox

    Maybe basic question, but how do I copy the last line of multiline textbox into another multiline textbox?
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  2. #2
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    556

    Re: Copy the last line from textbox into another textbox

    Split the text by Chr(10) (line feed) and get last line:

    VB.NET Code:
    1. Dim lines() = TextBox1.Text.Split(Chr(10))
    2. Dim lastLine = lines.Last()

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Copy the last line from textbox into another textbox

    Quote Originally Posted by peterst View Post
    Split the text by Chr(10) (line feed) and get last line:

    VB.NET Code:
    1. Dim lines() = TextBox1.Text.Split(Chr(10))
    2. Dim lastLine = lines.Last()
    Thanks, it works perfectly!
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,181

    Re: [RESOLVED] Copy the last line from textbox into another textbox

    The TextBox control also has a "Last" method

    Code:
    Me.TextBox1.Lines.Last

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: [RESOLVED] Copy the last line from textbox into another textbox

    Quote Originally Posted by wes4dbt View Post
    The TextBox control also has a "Last" method
    That's not actually true. The TextBox has a Lines property that returns a String array containing the lines of text. That Last method is an extension method that is a member of the Enumerable class and can be called on any IEnumerable(Of T) object:
    vb.net Code:
    1. Dim lines As String() = TextBox1.Lines
    2. Dim lastLine As String = lines.Last()
    It's the same Last method that was used in post #2. It's just that using Lines is better than calling Split on the Text yourself. Of course, I'd tend to do the whole lot in one go:
    vb.net Code:
    1. TextBox2.Text = TextBox1.Lines.Last()

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Copy the last line from textbox into another textbox

    Quote Originally Posted by peterst View Post
    Split the text by Chr(10) (line feed) and get last line:

    VB.NET Code:
    1. Dim lines() = TextBox1.Text.Split(Chr(10))
    2. Dim lastLine = lines.Last()
    Actually, that code is bad. You are splitting only on line feed characters so you are leaving the carriage returns in the results. That could cause problems if you ever need to compare Strings or in various other situations. If you were going to use Split, you should split on all possible line breaks. Using the Lines property will do that automatically.

  7. #7
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    556

    Re: Copy the last line from textbox into another textbox

    Quote Originally Posted by jmcilhinney View Post
    Actually, that code is bad. You are splitting only on line feed characters so you are leaving the carriage returns in the results. That could cause problems if you ever need to compare Strings or in various other situations. If you were going to use Split, you should split on all possible line breaks. Using the Lines property will do that automatically.
    You are right. I just translated first C# answer I've found (not using Google ) to VB.NET without testing and without any IDE or compiler around.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Copy the last line from textbox into another textbox

    Quote Originally Posted by peterst View Post
    You are right. I just translated first C# answer I've found (not using Google ) to VB.NET without testing and without any IDE or compiler around.
    The code you had would probably work for a RichTextBox, which uses just line feeds by default, I think. If you do want to split a String on line breaks, a thorough option be be like so:
    vb.net Code:
    1. Dim lines = myString.Split({ControlChars.CrLf,
    2.                             ControlChars.Cr,
    3.                             ControlChars.Lf},
    4.                            StringSplitOptions.None)
    That will split on any CR-LF pairs first, then any lone CR or LF.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Copy the last line from textbox into another textbox

    Quote Originally Posted by jmcilhinney View Post
    Actually, that code is bad. You are splitting only on line feed characters so you are leaving the carriage returns in the results. That could cause problems if you ever need to compare Strings or in various other situations. If you were going to use Split, you should split on all possible line breaks. Using the Lines property will do that automatically.
    No, it works perfectly! Line breaks are removed prior using another code.
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Copy the last line from textbox into another textbox

    Quote Originally Posted by VB.NET Developer View Post
    No, it works perfectly! Line breaks are removed prior using another code.
    Thank you for lying to us then. You specifically asked how to copy the last line from a TextBox into another TextBox but if you have already got the data out of the first TextBox and processed it then that's not actually the question you wanted answered. A lot of people are quite poor at identifying exactly where their real issue is, which is often why they can't find a solution for themselves, i.e. they're searching for the wrong thing. If your actual question was how to get the last element from an array or something like that then that's the question you should have asked.

    Apart from that, if anyone else finds this thread with a web search because they really do want to get the last line from one TextBox into another TextBox and they use that code, they will have a problem because it will not work perfectly.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Copy the last line from textbox into another textbox

    Quote Originally Posted by jmcilhinney View Post
    Thank you for lying to us then. You specifically asked how to copy the last line from a TextBox into another TextBox but if you have already got the data out of the first TextBox and processed it then that's not actually the question you wanted answered. A lot of people are quite poor at identifying exactly where their real issue is, which is often why they can't find a solution for themselves, i.e. they're searching for the wrong thing. If your actual question was how to get the last element from an array or something like that then that's the question you should have asked.

    Apart from that, if anyone else finds this thread with a web search because they really do want to get the last line from one TextBox into another TextBox and they use that code, they will have a problem because it will not work perfectly.
    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))    - this removes newline
    TextBox_Words.Text = TextBox_Words.Text.Remove(TextBox_Words.Text.LastIndexOf(Environment.NewLine))    - this removes wrong word (with _ at the beginning)
    Dim lines() = TextBox_Words.Text.Split(Chr(10))
    Dim lastLine = lines.Last()
    ProcessedWords.Text += lastLine + vbNewLine
    TextBox_Words.Clear()
    Timer1.Stop()
    GC.Collect()
    Last edited by VB.NET Developer; Aug 16th, 2020 at 09:56 AM. Reason: added explanation of "textbox_words"
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Copy the last line from textbox into another textbox

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width