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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Richtextbox questions

Status
Not open for further replies.

formerTexan

Programmer
Apr 10, 2004
504
US
I have been saving up some related questions regarding RichTextBoxes (RTB), so here goes:

1. It looks like RTB's no longer come as an ActiveX add in in A2003. Have they been replaced with something new and improved? Or am I missing something somewhere.

2. Peculiar behavior: I have a form with an RTB control and native Access textbox, both bound to the same memo field. The field contains some copy/pasted email text. As soon as I add the RTB control to the form, all of the html/xml tags from the original email text become visible in the native textbox control. Very yucky. I hadn't even realized the they had come along into the database. Has anyone else run into this oddity and have any guesses as to the reasons why.

3. RTB's allow portions of text strings to be selectively colored (highlighted). Very nice and saves a lot of API coding. However I would find it useful to be able to remove the highlights based on color. For example: change all red text to black text. The following bit of code will do this, but gets slow as it loops through the entire text string making a character by character comparision. This becomes excruciatingly slow by the time the text string hits about 15,000 characters. I haven't tweaked the code and I can probably think of something to gain a little performance, but does anyone have any ideas for a radical improvement. API?


Dim objRTB As Object 'RichTextBox
Dim lngLen As Long
Dim lng As Long
Set objRTB = Me.xtxtItem
lngLen = Len(objRTB.Text)

DoCmd.Echo False
With objRTB
For lng = 1 To lngLen
.SelLength = 1
If .SelColor = 255 Then
.SelColor = Not 255
.SelBold = False
End If
Next lng
.SelStart = 1
End With
DoCmd.Echo True
Set objRTB = Nothing


Thanks much.
Bill
 
Hi Bill,

I would say the quickest way to change the colour in the RTB is to use the .TextRTF property of that RTB.

For example you have an RTB with the text (originally [wink]) of RichTextBox1 the TextRTF would appear as:
Code:
{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
{\colortbl ;\red0\green255\blue255;}
\viewkind4\uc1\pard\cf1\lang2057\f0\fs17 RichTextBox1\cf0 
\par }
The part to look at here is the colortbl line. That shows there are two colours (semi-colon delimited) to choose from currently in the box, cf0 (which doesn't show but it seems is always black) and cf1 (displayed with it's RGB value \red0\green255\blue255;). A really quick way (seems we know what's there) is to replace all instances of cf1 with cf0. The TextRTF will also remove the reference to the Cyan in the colortbl. For example:
Code:
RichTextBox1.TextRTF = Replace(RichTextBox1.TextRTF, "cf1", "cf0")
would change all of the Cyan characters to black and change the TextRTF to:
Code:
{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\lang2057\f0\fs17 RichTextBox1
\par }
Once you have that basic concept it's easy to adapt it to cater for more colours and to replace highlights with other colours (both existing in the TextRTF and newly added).

I know this isn't the most in-depth explanation ofhow to use the TextRTF property but it should give an idea as to how to achieve point 3 quickly.

This is a VB6 example but if you just change RichTextBox1 to objRTB it should work straight in your code.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Harley,

Thanks much for the on-target suggestion. Once I figured out how the formatting was maintained, my thoughts ran along the same line.


For anybody that hasn’t used RichTextBox (RTB) before, I will cover a little bit of functionality and expand a bit on Harley’s useful suggestion. RTB’s use tags superficially similar to XML/HTML tags to maintain formatting (color, bold, font). There will be a header as well as tags inserted within the text to indicate specific formatting for text portions. At some point you will notice that RTB’s maintain formatting from session to session, so obviously the formatting information must be stored somewhere. The where turns out to be back in whichever field was set for the RTB’s controlsource. Open up the appropriate table and look at the field’s contents and you will see any formatting tags.

RTB controls can ignore the formatting and just display the text, but native Access controls can’t and will disconcertingly display the formatting tags.
Aside from the visual aggravation, this also presents potential problems if you are programmatically manipulating the text string via SQL orVBA as the tags are read as part of the text. So unless you specifically wish to maintain this formatting information between sessions, I suggest getting rid of it.

Two RTB properties are:

.Text
.TextRTF

A good way to see the difference is to run the following code from a form with a RTB control (named xRTB in this example). What you will see is that the .TextRTF property returns both the text and the RTF formatting. The Text property loses the formatting tags. You will also see a substantial difference in the string length.

Code:
Dim objRTB As Object  'RichTextBox
Dim lngLen As Long
Dim lngLenRTF As Long
Dim s As String
Set objRTB = Me.xRTB
    lngLen = Len(objRTB.Text)
    lngLenRTF = Len(objRTB.TextRTF)
    Debug.Print lngLen & " : "; objRTB.Text
    Debug.Print lngLenRTF; " : " & objRTB.TextRTF
Set objRTB = nothing

The following code will delete the formatting tags. Place it on a command button’s click event and try it.

Code:
Dim objRTB As Object  'RichTextBox
Dim s As String
Set objRTB = Me.xRTB

‘clean up formatting
s = objRTB.Text
If Len(s) = 0 Then
Me.xRTB = Null	‘in case the field(control source) won’t accept zls’s
Else
Me.xRTB = s
End If
Me.Refresh		‘update changes

How you actually implement this and where you place the preceding bit of code is up to you and your design needs, but using the form’s BeforeUpdate event is a good place to keep things clean. If you use BeforeUpdate, be sure to remove the Me.Refresh line Additionally you can make the cleanup conditional by adding something like an option box or pop up a message box to allow the user to decide whether to maintain formatting for individual records.

Cheers,
Bill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top