copy last line of richtextbox and paste into another

S

Sofa_snake

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
 
C

Cowboy \(Gregory A. Beamer\)

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://feeds.feedburner.com/GregoryBeamer

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top