-
Aug 15th, 2020, 01:46 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Aug 15th, 2020, 01:56 PM
#2
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:
Dim lines() = TextBox1.Text.Split(Chr(10))
Dim lastLine = lines.Last()
-
Aug 15th, 2020, 02:02 PM
#3
Thread Starter
Hyperactive Member
Re: Copy the last line from textbox into another textbox
Originally Posted by peterst
Split the text by Chr(10) (line feed) and get last line:
VB.NET Code:
Dim lines() = TextBox1.Text.Split(Chr(10)) 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.
-
Aug 15th, 2020, 03:35 PM
#4
Re: [RESOLVED] Copy the last line from textbox into another textbox
The TextBox control also has a "Last" method
Code:
Me.TextBox1.Lines.Last
-
Aug 15th, 2020, 11:33 PM
#5
Re: [RESOLVED] Copy the last line from textbox into another textbox
Originally Posted by wes4dbt
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:
Dim lines As String() = TextBox1.Lines
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:
TextBox2.Text = TextBox1.Lines.Last()
-
Aug 15th, 2020, 11:35 PM
#6
Re: Copy the last line from textbox into another textbox
Originally Posted by peterst
Split the text by Chr(10) (line feed) and get last line:
VB.NET Code:
Dim lines() = TextBox1.Text.Split(Chr(10))
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.
-
Aug 16th, 2020, 12:01 AM
#7
Re: Copy the last line from textbox into another textbox
Originally Posted by jmcilhinney
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.
-
Aug 16th, 2020, 12:31 AM
#8
Re: Copy the last line from textbox into another textbox
Originally Posted by peterst
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:
Dim lines = myString.Split({ControlChars.CrLf,
ControlChars.Cr,
ControlChars.Lf},
StringSplitOptions.None)
That will split on any CR-LF pairs first, then any lone CR or LF.
-
Aug 16th, 2020, 05:59 AM
#9
Thread Starter
Hyperactive Member
Re: Copy the last line from textbox into another textbox
Originally Posted by jmcilhinney
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.
-
Aug 16th, 2020, 06:34 AM
#10
Re: Copy the last line from textbox into another textbox
Originally Posted by VB.NET Developer
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.
-
Aug 16th, 2020, 09:47 AM
#11
Thread Starter
Hyperactive Member
Re: Copy the last line from textbox into another textbox
Originally Posted by jmcilhinney
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.
-
Aug 16th, 2020, 10:00 AM
#12
Re: Copy the last line from textbox into another textbox
Originally Posted by VB.NET Developer
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:
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
ought to be replaced with this:
vb.net Code:
Dim lines = TextBox_Words.Lines
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|