copy last line of richtextbox and paste into another

copy last line of richtextbox and paste into another

Post by Sofa_snak » Thu, 22 Jan 2009 21:36:55


Hello
I have a richtextbox 1 that updates with all commands sent and
recieved from RS232 communication.

I have a 2nd box which is also a richtextbox. I want to copy the last
line of richtextbox 1 and place it in richtextbox2. A previous
instruction view window if you like.

I seem to finding this task quite difficult!
Could anybody help?? thank you!

I see that the .lines expression returns each line in my textbox as a
string but then I cant operate on a particular line!? is there an
easier way?

Thank you

Dan
 
 
 

copy last line of richtextbox and paste into another

Post by Gregory A. » Fri, 23 Jan 2009 02:24:54

Does

richTextBox2.Text = richTextBox1.Lines(richTextBox1.Lines.Length - 1)

not work?

If not, make sure the final line is not blank. Here is an easy way to do
this:

Dim lastLine As String

lastLine = richTextBox1.Lines(richTextBox1.Lines.Length - 1)

If (lastLine = String.Empty) Then
lastLine = richTextBox1.Lines(richTextBox1.Lines.Length - 2)
End if

This makes an assumption which may be flawed. That is that the last line is:

"Line X" + vbCrLf

and not

"Line X" + vbCrLf + vbCrlf

This is a decent assumption with many text files. The RTF textbox sees after
vbCrLf as a separate line. In C#, it is handle a bit better, as it is seen
as a null, not an empty string.

If you want to get rid of situations where there are multiple vbCrLf
situations, you will have to read through lines until you get a hit, with
some protection that there are actually lines in the RTF.


--
Gregory A. Beamer
MVP; MCP: +I, Se, SD, DBA

Blog:
http://www.yqcomputer.com/

*************************************************
| Think outside the box! |
*************************************************