I'm having a few problems with the RTB control, and no ammount of googling or forum-sniffing brought the requred info.
First of all, how can I highlight the selected text(with a color, not the default)? On this forum, I've stumpled upon a technique of drawing a texbox with the text and the fill color as the background, but it's very inelegant, and hard to get right(calculating the position on variable font\box sizes is really annoying). I know RTF files are capable of holding hilight info, and the control is capable of displaying is, but how to apply it in runtime...?
Next, is there a way to detect how long is a visible line(not anything ending with a line break, but the one displayed in the control)? The font I'm using is Times New Roman, so it won't be possible to check the length by getting the characters. And words are wrapped anyway.
Thanx in advance.
Last edited by Max_aka_NOBODY; Apr 14th, 2005 at 07:59 AM.
can I highlight the selected text(with a color, not the default)? On this forum, I've stumpled upon a technique of drawing a texbox with the text and the fill color as the background, but it's very inelegant, and hard to get right(calculating the position on variable font\box sizes is really annoying). I know RTF files are capable of holding hilight info, and the control is capable of displaying is, but how to apply it in runtime...?
Try something like this
VB Code:
Dim arrStringArray() As String
Dim lngPos As Long
Dim lngLoop As Long
With Richtextbox1
arrStringArray() = Split(.Text, vbCrLf)
For lngLoop = 0 To UBound(arrStringArray)
If InStr(arrStringArray(lngLoop), "Words Or String to Highlight") Then
.SelStart = lngPos
.SelLength = Len(arrStringArray(lngLoop))
.SelBold = True
.SelColor = vbRed
End If
lngPos = .Find(vbCrLf, lngPos) + 2
Next
End With
You can put that into a sub and pass it the string to highlight along with the color of choice
Thanx. Nice tutorial there, Bruce, but it still doesn't answer my main question.
Thanx for taking the time to write the code, Hack, but you've misunderstood my question. I'm asking for a way to add highlighting, by which I mean fill color. I don't mind using SendMessage -- or any other API to that matter -- as it seems that the RTF doesn't have the functionality I need exposed, or is it just my ignorance?
Sorry Max, I did not understand originally that you were looking for the backcolor (fill color / highlight) as opposed to the forecolor or text color. I did not look at Dave's example yet, but hopefully he has what you need.