Click here to Skip to main content
14,640,670 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear Sir

i am changing the color of rich text box using this code
but when i enter the new line it change the color of above lines.i just want to change the color of current row.
first time font color of row one is red
second time the font color of row two is green
but third time the font color of second row change from green to again red
please help me out

Dim start1 As Integer
start1 = Len(RichTextBox1.Text)
If RichTextBox1.Text <> "" Then
RichTextBox1.Text = RichTextBox1.Text & vbCrLf & Text1.Text
With RichTextBox1
.SelStart = start1

.SelLength = Len(RichTextBox1)
.SelColor = vbGreen

End With
Else
RichTextBox1.Text = Text1.Text
With RichTextBox1
.SelStart = start1
.SelLength = Len(RichTextBox1)

.SelColor = 255

End With
End If
End Sub


thanks sir
Posted
Updated 23-May-14 10:19am
v3
Comments
CHill60 22-May-14 8:49am
   
Code?
rajbir singh thind 22-May-14 8:55am
   
str(0) = 10001
str(1) = 10002
str(2) = 10003
While iDay <= iEndIndex
If iDay = 1 Then
For i = 0 To 2
start1 = Len(txtdays(0).Text)

'RichTextBox1.Text = RichTextBox1.Text & vbCrLf & Text1.Text
If i = 0 Then
txtdays(0).Text = iDay
With txtdays(0)

' .Font.Size = 20
'.Font.Bold = True
.SelStart = start1
'.Span vbCr, True, True
.SelLength = Len(txtdays(0))
.SelColor = vbRed
.SelFontSize = 25
.SelBold = True
End With
ElseIf i = 1 Then
txtdays(0).Text = txtdays(0).Text & vbCrLf & str(i)

With txtdays(0)
.SelStart = start1


.Span vbCr, True, True
.SelLength = Len(txtdays(0))
.SelColor = vbGreen
.SelFontSize = 2
End With
ElseIf i = 2 Then
txtdays(0).Text = txtdays(0).Text & vbCrLf & str(i)

With txtdays(0)
.SelStart = start1


.SelLength = Len(txtdays(0))
.SelColor = vbBlue
.SelFontSize = 2
End With

'txtdays(0).Text = str(i)

End If
Next

End If



Wend
DamithSL 22-May-14 9:04am
   
please update the question by using Improve question
karthik Udhayakumar 22-May-14 13:00pm
   
@Rajbir,
Start writing better understandable questions along with the code to get better help from the community:( all the best

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Your main problem is the way you are adding text to your RTB
RichTextBox1.Text = RichTextBox1.Text & vbCrLf & Text1.Text

Text is, well ... just text. It doesn't have any concept of colour, format etc. For example compare NotePad (a text editor) to something like MS Word (a rich text editor)

The next problem is the way you are selecting .SelLength
.SelLength = Len(RichTextBox1)
Firstly - Are you absolutely sure sure that the default property of this control is its Text property?
.SelLength = Len(RichTextBox1.Text)
would have been better, but still wouldn't fix your problem as you are selecting the entire text of the RTB.

You will be better off using the .SelText property of the RTB ... set the start point of the selection, set the colour you want, then set the selected text to be the information you want to insert. Like this
Dim start1 As Integer
Dim lCurrColour As Long

    start1 = Len(RichTextBox1.Text)
    If RichTextBox1.Text <> "" Then

        With RichTextBox1
            .SelStart = start1
            lCurrColour = .SelColor
            .SelColor = vbGreen
            .SelText = vbCrLf & Text1.Text
            .SelColor = lCurrColour
        End With
    Else
...etc
Notice that I capture the current colour at the point I'm going to insert the line - lCurrColour and then set it back after the insert.

If this was me I would refactor the code to avoid duplication of code/effort. I have a personal preference for an if statement being as close as possible to the thing that is affected by it (NB - personal preference - that's just me, others might disagree). I would have done something more like this
Dim lCurrColour As Long

    With RichTextBox1
        .SelColor = IIf(RichTextBox1.Text = "", 255, vbGreen)
        lCurrColour = .SelColor
        .SelStart = Len(RichTextBox1.Text)
        .SelText = Text1.Text & vbCrLf
        .SelColor = lCurrColour
    End With
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100