Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rich Text Box - Different Fonts/Colors Per Line

Status
Not open for further replies.

kmcclung

Programmer
Nov 20, 2001
120
0
0
US
I have a calendar where each day of the month is a rich text box. I am using it to display all the days of the month and then additional text for an "incident" that may have occurred on a particular day. It works great expect I'd like to change the font/justification for the date and the "incident".

When I initialize the calendar I loop through each day and see if I have a record in my "incident" collection that matches the date. If I do, I add the "incident" text to the existing text in the rich text box. This is a must because there can be more than one "incident" per day.

For example, on the 17th there may be 2 incidents. It currently displays as:

17
Late 1:30
Long 5:15

What I'd like is to right justify and bold the date (17) and then change the font of the remaining text.

I'm adding the text to the rich text box with the following code:

txtDay(i).Text = iDay 'iDay = 17 in this case

If Format$(oIncident.IncDate, "dd") = Format$(iDay, "00") Then
txtDay(i).Text = txtDay(i).Text & vbCrLf & _
oIncident.IncCode & " " & _

Hope this makes sense...



 
That works great to apply to all the text, but where I'm having the trouble is doing it for some lines in the rich text box.
 
Try this.

txtDay(i).SelStart = InStr(txtDay(i).Text, oIncident.IncDate) + Len(oIncident.IncDate)

txtDay(i).SelLength = Len(txtDay(i).Text) - InStr(txtDay(i).Text, oIncident.IncDate) + Len(oIncident.IncDate)

txtDay(i).SelColor = vbRed
 
You want to align too so try this.

txtDay(i).SelStart = 0
txtDay(i).SelLength = InStr(txtDay(i).Text, oIncident.IncDate) + Len(oIncident.IncDate)
txtDay(i).SelAlignment = rtfRight

txtDay(i).SelStart = InStr(txtDay(i).Text, oIncident.IncDate) + Len(oIncident.IncDate)
txtDay(i).SelLength = Len(txtDay(i).Text) - InStr(txtDay(i).Text, oIncident.IncDate) + Len(oIncident.IncDate)
txtDay(i).SelColor = vbRed
 
I'm not sure what the first 2 lines are doing, but the last line changes all text in the rich text box to red.
 
The 1st sets the selection start location to right after the string oIncident.IncDate. Then the 2nd says to make the selection length = to the rest of the Textbox and finally the last says to change the color of the selected text to red.

This would have to be ran after the textbox has been populated.

To test this make another textbox named RichTextBox1
add a Command button named Command1. Add the code below, then run the app and type in the textbox "Hello 'some more text'" and it should change everything after Hello to red

Private Sub Command1_Click()
'RichTextBox1.SelAlignment
RichTextBox1.SelStart = InStr(RichTextBox1.Text, "Hello") + Len("Hello")
RichTextBox1.SelLength = Len(RichTextBox1.Text) - InStr(RichTextBox1.Text, "Hello") + Len("Hello")
RichTextBox1.SelColor = vbRed
End Sub
 
It does hightlight the rest of the text in black but all of the text is red.
The test worked so I'll play with it a little more. Thanks so much for your help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top