Click to See Complete Forum and Search --> : RichTextBox Color Preservation


Tempest3D
April 30th, 2001, 08:21 PM
I currently have the following code

private Sub Command1_Click()
RichTextBox1.Text = RichTextBox1.Text & vbNewLine & Text1.Text
RichTextBox1.SelStart = InStr(len(RichTextBox1.Text) - len(Text1.Text), RichTextBox1.Text, Text1.Text) - 1
RichTextBox1.SelLength = len(Text1.Text)
If Option1.Value = true then
RichTextBox1.SelColor = vbRed
else
RichTextBox1.SelColor = vbBlue
End If
RichTextBox1.SelLength = 0
End Sub



(where option1 says red and option2 says blue)
(the way it works is you type text in Text1, and it adds that to RichText1, in the selected color)

Eveytime I click command1, the whole textbox resets itself to black, and only the last thing i typed in has any color. What do I need to do?

shree
April 30th, 2001, 09:46 PM
Here's one way to get about it


private Sub Command1_Click()
Dim OldLen as Integer
OldLen = len(RichTextBox1.Text)
RichTextBox1.SelStart = OldLen
RichTextBox1.SelText = vbCrLf
OldLen = len(RichTextBox1.Text)
RichTextBox1.SelText = Text1.Text
RichTextBox1.SelStart = OldLen
RichTextBox1.SelLength = len(Text1.Text)
If Option1.Value = true then RichTextBox1.SelColor = vbRed else RichTextBox1.SelColor = vbBlue
RichTextBox1.SelLength = 0
End Sub

Tempest3D
May 1st, 2001, 09:34 PM
Hey, would it be too much to ask if you could explain what this is doing? Ive been looking at it and I'm totally lost.
Thanks.

shree
May 1st, 2001, 10:04 PM
private Sub Command1_Click()
Dim OldLen as Integer
'Save the length of the text
OldLen = len(RichTextBox1.Text)
'set the insertion point at the end of the the rich text box
RichTextBox1.SelStart = OldLen
'Insert a newline character at the end, you could use vbNewLine
RichTextBox1.SelText = vbCrLf
'Compute the newlength. It is basically oldlen + 2
OldLen = len(RichTextBox1.Text)
'This adds the new text at the end of what is already in the rtb.
RichTextBox1.SelText = Text1.Text
'Selection starts at the old length (just before the new text)
RichTextBox1.SelStart = OldLen
'You know this...
RichTextBox1.SelLength = len(Text1.Text)
'Just wrote your three five lines of If...then in one line
If Option1.Value = true then RichTextBox1.SelColor = vbRed else RichTextBox1.SelColor = vbBlue
'Finally remove the selection
RichTextBox1.SelLength = 0
End Sub