Answered by:
Rich text box exorcism needed!!!

Question
-
After a suggestion from the forum members to use a Rich Text box as a solution to my project, I run into something that makes me scared to use it because it seems to have supernatural powers.
I place the following code into two buttons:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click tb1.Text = rtbSource.Text End Sub Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click rtbSource.Clear() rtbSource.Text = tb1.Text End Sub
I enter the following text into the Rich Text Box:
The Rich Text Box has two paragraphs with two lines in each Paragraph!!!
When I press the save button, the text moved into the normal text box looks like this:
The Rich Text boxhas two paragraphswith two lines ineach paragraph!!!
This is surprising but reasonable but what really seems like magic is when I press the btn restore which clears the rich text and then restores it to it's original form, reinserting the carriage returns at the proper places.
I would really appreciate one of you forum members helping me exorcise the magic out of this situation since I can't even imagine what mysterious forces allows the rich text box to remember it's original formatting after it is cleared. There is obviously something I don't understand other than we are approaching haloween and I will really appreciate any comments on this.
Jerry
- Edited by Jercook Thursday, September 29, 2011 4:58 PM
Thursday, September 29, 2011 4:45 PM
Answers
-
The RTB uses LF for new lines whereas the Textbox needs CR+LF. The textbox just ignores LF but it's still there as you can see after moving text back to RTB.
Armin- Proposed as answer by John Anthony Oliver Thursday, September 29, 2011 5:39 PM
- Marked as answer by Jercook Thursday, September 29, 2011 5:59 PM
Thursday, September 29, 2011 5:14 PM -
Hi Jercook,
Just to add to the reply from Armin above.
When assigning text from a RichTextBox to a TextBox you can add
.Replace(vbLf, vbCrLf)
Like this:>>
Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load RichTextBox1.Text = "The Rich Text box" & vbCrLf & "has two paragraphs" & vbCrLf & vbCrLf & "two lines in" & vbCrLf & "each paragraph!!!" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Text = RichTextBox1.Text.Replace(vbLf, vbCrLf) End Sub End Class
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Marked as answer by Jercook Thursday, September 29, 2011 5:58 PM
- Edited by John Anthony Oliver Thursday, September 29, 2011 6:10 PM
Thursday, September 29, 2011 5:42 PM -
Yep, voted as helpful. In addition: this is not necessary in the other direction, i.e. if you assign a String containing CR+LF to RTB.Text, the RTB automatically replaces CR+LF with LF only.
Armin- Marked as answer by Jercook Thursday, September 29, 2011 5:58 PM
Thursday, September 29, 2011 5:49 PM
All replies
-
The RTB uses LF for new lines whereas the Textbox needs CR+LF. The textbox just ignores LF but it's still there as you can see after moving text back to RTB.
Armin- Proposed as answer by John Anthony Oliver Thursday, September 29, 2011 5:39 PM
- Marked as answer by Jercook Thursday, September 29, 2011 5:59 PM
Thursday, September 29, 2011 5:14 PM -
Hi Jercook,
Just to add to the reply from Armin above.
When assigning text from a RichTextBox to a TextBox you can add
.Replace(vbLf, vbCrLf)
Like this:>>
Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load RichTextBox1.Text = "The Rich Text box" & vbCrLf & "has two paragraphs" & vbCrLf & vbCrLf & "two lines in" & vbCrLf & "each paragraph!!!" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Text = RichTextBox1.Text.Replace(vbLf, vbCrLf) End Sub End Class
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Marked as answer by Jercook Thursday, September 29, 2011 5:58 PM
- Edited by John Anthony Oliver Thursday, September 29, 2011 6:10 PM
Thursday, September 29, 2011 5:42 PM -
Yep, voted as helpful. In addition: this is not necessary in the other direction, i.e. if you assign a String containing CR+LF to RTB.Text, the RTB automatically replaces CR+LF with LF only.
Armin- Marked as answer by Jercook Thursday, September 29, 2011 5:58 PM
Thursday, September 29, 2011 5:49 PM -
Thanks for the answer. This one was really puzzling. Amazing how such puzzling situations ca have such a reasonable explanation when when you finally understand what is going on.
Thanks so much to all of you.
Jerry
Thursday, September 29, 2011 6:01 PM