Home > Archive > Visual Basic > May 2004 > Text Color in richText
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Text Color in richText
|
|
| Thomas Scheiderich 2004-05-29, 4:30 pm |
| In a richtext control, can set a line of text to be a different color.
I am displaying database records in my richtext box and I have certain lines
I would like to stand out. At the moment I am just putting displaying my
line like so:
****************************************
************************************
**
stemp = Format(" ", "!@@@@@@") & " " & _
Format("Totals ", "!@@@@@@@@@@@@@@@") & _
" " & Format(" ", "!@@") & " " & _
Format(" ", "!@@@@@@") & " " & _
Format(Format(GrandTotalPayHours, "###0.00"), "@@@@@@@") & _
" " & Format(Format(GrandTotalAdjPayHours, "###0.00"),
"@@@@@@@") & _
" " & Format(Format(" ", "@@@@@@@"), "@@@@@@@") & " " & _
Format(Format(GrandTotalOT, "###0.00"), "@@@@@@@") & " " & _
Format(Format(GrandTotalDT, "###0.00"), "@@@@@@@") & " " & _
Format(Format(GrandTotalShiftPremium, "###0.00"), "@@@@@@@") & "
" & _
Format(Format(GrandTotalHolidays, "###0.00"), "@@@@@@@") & " " &
_
Format(Format(GrandTotalHoursWorked, "####0.00"), "@@@@@@@@")
TextHelp.SelStart = Len(TextHelp.Text)
TextHelp.SelText = stemp & vbCrLf
****************************************
************************************
**
Is there a way to set this line to red then set it back to black.
I noticed in the properties, there is background color, but couldn't find a
Textcolor or foreground color.
Thanks,
Tom.
| |
|
| Look into the other .Sel properties like:
..SelColor
..SelBold
..SelUnderline
..SelItalic
..SelFontSize
In your case:
With TextHelp
.SelStart = Len(.Text)
.SelColor = vbRed
.SelText = stemp & vbCrLf
.SelColor = vbBlack
End With
I'm not sure what you mean by setting it to red and then back to black. Do
you want a single line to be red at one time and black at another or are you
just referring to switching from displaying text red or black...
--
Chris Hanscom
MVP (Visual Basic)
http://www.veign.com
--
"Thomas Scheiderich" <tfs@deltanet.com> wrote in message
news:10bhnnb7vh9ac35@corp.supernews.com...
> In a richtext control, can set a line of text to be a different color.
>
> I am displaying database records in my richtext box and I have certain
lines
> I would like to stand out. At the moment I am just putting displaying my
> line like so:
>
>
****************************************
************************************[col
or=darkred]
> **
> stemp = Format(" ", "!@@@@@@") & " " & _
> Format("Totals ", "!@@@@@@@@@@@@@@@") & _
> " " & Format(" ", "!@@") & " " & _
> Format(" ", "!@@@@@@") & " " & _
> Format(Format(GrandTotalPayHours, "###0.00"), "@@@@@@@") & _
> " " & Format(Format(GrandTotalAdjPayHours, "###0.00"),
> "@@@@@@@") & _
> " " & Format(Format(" ", "@@@@@@@"), "@@@@@@@") & " " &[/color]
_
> Format(Format(GrandTotalOT, "###0.00"), "@@@@@@@") & " " & _
> Format(Format(GrandTotalDT, "###0.00"), "@@@@@@@") & " " & _
> Format(Format(GrandTotalShiftPremium, "###0.00"), "@@@@@@@") &
"
> " & _
> Format(Format(GrandTotalHolidays, "###0.00"), "@@@@@@@") & " "
&
> _
> Format(Format(GrandTotalHoursWorked, "####0.00"), "@@@@@@@@")
>
> TextHelp.SelStart = Len(TextHelp.Text)
> TextHelp.SelText = stemp & vbCrLf
>
****************************************
************************************[col
or=darkred]
> **
> Is there a way to set this line to red then set it back to black.
>
> I noticed in the properties, there is background color, but couldn't find[/color]
a
> Textcolor or foreground color.
>
> Thanks,
>
> Tom.
>
>
| |
| Thomas Scheiderich 2004-05-29, 6:30 pm |
| "Veign" <NOSPAMinveign@veign.com> wrote in message
news:esl9rIbREHA.904@TK2MSFTNGP12.phx.gbl...
> Look into the other .Sel properties like:
> .SelColor
> .SelBold
> .SelUnderline
> .SelItalic
> .SelFontSize
>
> In your case:
> With TextHelp
> .SelStart = Len(.Text)
> .SelColor = vbRed
> .SelText = stemp & vbCrLf
> .SelColor = vbBlack
> End With
>
> I'm not sure what you mean by setting it to red and then back to black.
Do
> you want a single line to be red at one time and black at another or are
you
> just referring to switching from displaying text red or black...
>
Most of the lines will be black, but there will be the odd line that I want
to be red so it stands out.
After I do your setup, do I need to unselect somehow, or will the the new
line be selected.
Thanks,
Tom.
> --
> Chris Hanscom
> MVP (Visual Basic)
> http://www.veign.com
> --
>
> "Thomas Scheiderich" <tfs@deltanet.com> wrote in message
> news:10bhnnb7vh9ac35@corp.supernews.com...
> lines
my[color=darkred]
>
****************************************
************************************[col
or=darkred]
&
> _
&[color=darkred]
> "
"[color=darkred]
> &
"@@@@@@@@")[color=darkred]
>
****************************************
************************************[col
or=darkred]
find
> a
>
>
| |
|
| SelStart property selects the starting point of the SelText text. Give it a
try and see what happens..
--
Chris Hanscom
MVP (Visual Basic)
http://www.veign.com
--
"Thomas Scheiderich" <tfs@deltanet.com> wrote in message
news:10bi1el18j96j34@corp.supernews.com...
> "Veign" <NOSPAMinveign@veign.com> wrote in message
> news:esl9rIbREHA.904@TK2MSFTNGP12.phx.gbl...
> Do
> you
>
> Most of the lines will be black, but there will be the odd line that I
want
> to be red so it stands out.
>
> After I do your setup, do I need to unselect somehow, or will the the new
> line be selected.
>
> Thanks,
>
> Tom.
> my
>
****************************************
************************************[col
or=darkred]
_
"
> &
_[color=darkred]
_[color=darkred]
"@@@@@@@")[color=darkred]
> &
"[color=darkred]
> "
> "@@@@@@@@")
>
****************************************
************************************[col
or=darkred]
> find
>
>
| |
| Rick Rothstein 2004-05-29, 7:30 pm |
| > I noticed in the properties, there is background color,
> but couldn't find a Textcolor or foreground color.
You didn't heed my suggestion to look at the other SelXXX properties,
did you?<g>
>
> Most of the lines will be black, but there will be the
> odd line that I want to be red so it stands out.
>
> After I do your setup, do I need to unselect somehow,
>or will the the new line be selected.
There are two ways you can use SelColor (they are actually the same, but
they "appear" different). In the first method, you can place all of your
text and then go back, select some text that has already been placed and
set its color to something else. For example,
With RichTextBox1
.Text = "Line one" & vbCrLf & "Line Two" & _
vbCrLf & "Line Three"
.SelStart = InStr(.Text, vbCrLf) + 1
.SelLength = Len("Line Two")
.SelColor = vbRed
.SelStart = .SelStart + .SelLength
End With
The purpose of that last line is to remove the highlight (selection)
which can be done by either setting the SelStart property to a new value
or setting SelLength to zero. The value I used makes sure the text caret
ends up at the end of the selection that is being removed (I thought it
would look nicer if the caret was at the end of the text that was just
made red).
In the second method, you can color the text as it is placed. For
example,
With RichTextBox1
.Text = ""
.SelColor = vbBlack
.SelText = "Line one" & vbCrLf
.SelColor = vbRed
.SelText = "Line Two" & vbCrLf
.SelColor = vbBlack
.SelText = "Line Three"
End With
The last color set will be used for each new line added thereafter.
Rick - MVP
| |
| Thomas Scheiderich 2004-05-31, 3:30 am |
| "Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:e5P0Q9cREHA.3628@TK2MSFTNGP12.phx.gbl...
>
> You didn't heed my suggestion to look at the other SelXXX properties,
> did you?<g>
No, I actually wasn't sure how it worked at the time. I do now. I hadn't
started playing with the richtext box yet.
BTW, can I change the background of just the line I am writing or only for
the whole box?
>
>
>
> There are two ways you can use SelColor (they are actually the same, but
> they "appear" different). In the first method, you can place all of your
> text and then go back, select some text that has already been placed and
> set its color to something else. For example,
>
> With RichTextBox1
> .Text = "Line one" & vbCrLf & "Line Two" & _
> vbCrLf & "Line Three"
> .SelStart = InStr(.Text, vbCrLf) + 1
> .SelLength = Len("Line Two")
> .SelColor = vbRed
> .SelStart = .SelStart + .SelLength
> End With
>
> The purpose of that last line is to remove the highlight (selection)
> which can be done by either setting the SelStart property to a new value
> or setting SelLength to zero. The value I used makes sure the text caret
> ends up at the end of the selection that is being removed (I thought it
> would look nicer if the caret was at the end of the text that was just
> made red).
I agree.
In this case, it doesn't really matter as I am just displaying the data as I
read it and there is no interaction with the user. Here is what I did after
I changed to the richtext box.
....
Format(Format(ShiftPremium, "###0.00"), "@@@@@@@") & " " & _
Format(Format(HolidayHoursPremium, "###0.00"), "@@@@@@@") &
" " & _
Format(Format(GrandTotalHoursWorked, "####0.00"), "@@@@@@@")
With TextHelp
TextHelp.SelStart = Len(TextHelp.Text)
.SelColor = vbRed
TextHelp.SelText = stemp & vbCrLf
.SelColor = vbBlack
End With
It works great.
Thanks,
Tom.
>
> In the second method, you can color the text as it is placed. For
> example,
>
> With RichTextBox1
> .Text = ""
> .SelColor = vbBlack
> .SelText = "Line one" & vbCrLf
> .SelColor = vbRed
> .SelText = "Line Two" & vbCrLf
> .SelColor = vbBlack
> .SelText = "Line Three"
> End With
>
> The last color set will be used for each new line added thereafter.
>
> Rick - MVP
>
| |
| Rick Rothstein 2004-05-31, 6:30 am |
| > BTW, can I change the background of just the line I am
> writing or only for the whole box?
The following is taken from a couple of other posts I've offered in the
past when the question of coloring the background of text in a
RichTextBox (VB's control doesn't provide for this directly) which I've
merged together here (which is why some of the wording between
paragraphs may seem awkward or choppy).
Rick - MVP
I got to thinking about this problem again and came up with a super
kludge (that is 'super' as in large, not fantastic<g> ) that seems to
work. The only proviso is that there cannot be any other highlighted
text in the file.
<original code not provided>
Okay, the code in my previous post works fine, but I've come up with a
variation that might be of interest to some of you out there. This
modification allows you to highlight several different words (or text
strings) with several different colors. It also provides a Clear
subroutine to remove all highlighting in the RichTextBox. (The code for
the two subroutines appear after my signature.) So, you might choose to
highlight all of the words "programmer" in your text with a light yellow
background, all of the words "Visual Basic" in light orange and all of
the words "skill" in light green... all at the same time. You would do
that like this (assuming CommandButton's for each highlight activation).
Private Sub Command1_Click()
RichTextBox1.SetFocus
RichTextBoxSelBkClr RichTextBox1, "programmer", _
vbYellow, False, True
End Sub
Private Sub Command2_Click()
RichTextBox1.SetFocus
RichTextBoxSelBkClr RichTextBox1, "skill", _
&HC0FFC0, False, True
End Sub
Private Sub Command3_Click()
RichTextBox1.SetFocus
RichTextBoxSelBkClr RichTextBox1, "Visual Basic", _
&HC0E0FF, True, True
End Sub
Then, when you were ready to remove all of the highlights, simply
execute this code
Private Sub Command5_Click()
RichTextBox1.SetFocus
ClearRichTextBoxBkClr RichTextBox1
End Sub
Although you might not want to return focus to your RichTextBox
depending on whether you are allowing the user to edit any text or not.
Also note that I've added a 6th parameter (it's Optional) to the
RichTextBoxSelBkClr subroutine based on the observation that Steve made.
The FreezeCursor argument controls whether the text scrolls to the last
occurence of the word(s) being searched for or not. The default value
for FreezeControl is true which means the text will Not be scrolled to
the last occurence. If you want it to scroll to the last occurence,
specify False for the argument.
NOTE... this solution is a kludge. It doesn't properly splice highlights
within highlights. I didn't figure out how to control all the back
references that take place under the hood in RTF when you imbed colors
within colors. Do you remember in my first post when I said "The only
proviso is that there cannot be any other highlighted text in the file"?
Well, that is why I said it; originally, I was only going to provide the
minimum functionality to answer the posted question. Then, I decided to
extend the functionality anyway. I meant to mention that proviso again
along with the warning about the highlight within highlight problem...
and I forgot. I'm glad you tripped over it and posted your
"challenge"... it gave me a chance to add this note for the archived
record.
Rick - MVP
Sub RichTextBoxSelBkClr(RTB As RichTextBox, _
Word As String, BkClr As Long, _
Optional MatchCase As Boolean = False, _
Optional WholeWord As Boolean = False, _
Optional FreezeCursor As Boolean = True)
Dim CStart As Long
Dim CEnd As Long
Dim Options As Long
Dim Position As Long
Dim CNum As Integer
Dim BkColor As String
Dim EColors As String
Dim RTF As String
Dim CTbl() As String
Dim CiLst() As String
If MatchCase Then Options = rtfMatchCase
If WholeWord Then Options = Options Or rtfWholeWord
BkColor = ";\red" & CStr(BkClr Mod 256) & _
"\green" & CStr(BkClr \ 256 Mod 256) & _
"\blue" & CStr(BkClr \ 65536)
With RTB
.Visible = False
.HideSelection = Not FreezeCursor
Position = .Find(Word, 0, , Options)
Do While Position > -1
.SelText = "\highlightx" & .SelText & "\highlight0 "
RTF = .TextRTF
CStart = InStr(RTF, "{\colortbl")
If CStart Then
CEnd = InStr(CStart, RTF, ";}")
EColors = Mid$(RTF, CStart, CEnd - CStart + 2)
CNum = UBound(Split(EColors, ";"))
Else
CNum = 1
RTF = Replace$(RTF, "{\fonttbl", _
"{\colortbl ;}{\fonttbl")
End If
RTF = Replace$(RTF, "\highlightx", _
"\highlight" & CStr(CNum))
CTbl = Split(RTF, "{\colortbl")
CiLst = Split(CTbl(1), ";}")
CiLst(0) = CiLst(0) & BkColor
CTbl(1) = Join(CiLst, ";}")
RTF = Join(CTbl, "{\colortbl")
.TextRTF = Replace$(RTF, "\\highlight", "\highlight")
Position = .Find(Word, Position + Len(Word), , Options)
Loop
.Visible = True
End With
End Sub
Sub ClearRichTextBoxBkClr(RTF As RichTextBox)
With RTF
Do While .TextRTF Like "*\highlight1 *"
RTF = Replace$(RTF, "\highlight1 ", "")
RTF = Replace$(RTF, "\highlight0 ", _
"\highlight1 \highlight0 ")
Loop
End With
End Sub
|
|
|
|
|