Link to home
Start Free TrialLog in
Avatar of mfrumer
mfrumer

asked on

Control of fonts in Rich Text Box

In my application I maintain text in many Rich Text Boxes. I allow the user to insert special characters using the Symbol font and other fonts. I produce printed output by assembling a Rich Text document from pieces of text from the various Rich Text Boxes. The document includes tables where I build cells which include pieces of text. Everything is working EXCEPT that when I pick up various pieces of text with SetRTF I get different assignments of fonts. That is, each piece of text has its own {\fonttbl...}}. This messes up other succeeding text. For exmaple, the document starts by using f2 for a normal font. Later I pick up text whose {\fonttbl...}} uses f2 for a Symbol font. From then on, all text which should be normal ends up as symbol. Is there some way that I can force VB to always use the same font numbers for the same fonts? Is there any "scope" of fonts in an RTF document, or do all fonts apply throughout a document?
Avatar of taplin
taplin

I do not think you can.  As far as I know, you'll need to write a parsing routine that will manage this.

Sorry...
Jay
What's your method of grabbing the rich text from the RichTextBoxes? Can you show us a little bit of the code so we can see the context in which you're currently doing this? I think I can help, but I want to make sure I understand correctly.

Let me know. Thanx.
Avatar of mfrumer

ASKER

Thanks. The following is one way that I build up the rich text. Because rich text boxes support only a subset of rich text commands, I sometimes insert a place holder in the rich text string. I then use a separate program to read the rich text file and replace the place holders with snippets of rich text.

The fundamental problem is how to allow users to create many separate pieces of rich text which are kept in data base fields and assembled into a document. I would welcome alternative approaches to the problem.

RichTextBox_Staging.TextRTF = _
  supplies_RTF(i,j)
Call SelectAllInStaging
Call AddToProcedure
..
..
..
Private Sub SelectAllInStaging()
RichTextBox_Staging.SelStart = 0
RichTextBox_Staging.SelLength =   Len(RichTextBox_Staging.text)
End Sub

Private Sub AddToProcedure()
RichTextBox_Procedure.SelStart =   Len(RichTextBox_Procedure.text)
RichTextBox_Procedure.SelRTF =    RichTextBox_Staging.SelRTF
End Sub
Is there a list of the main properties you want to retain? i.e. font, size, color, bold, italics, etc... Anything else?
ASKER CERTIFIED SOLUTION
Avatar of taplin
taplin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mfrumer

ASKER

Wow! You did quite a job, and I think that I am heading toward a solution. My fonts are less messed up than before, but there are still some problems.

Here is what appears to cause the problem, and I wondered if your routine was intended to address this.

In the beginning of the document, there is:

{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\qj\fswiss MS Sans Serif;}{\f1\fdecor\fcharset2 Symbol;}{\f2\qj\fswiss\fprq2 Verdana;}{\f3\qj\fswiss MS Sans Serif;}{\f4\froman\fprq2\fcharset2 Symbol;}}

followed by text which contain references to the various font numbers.

Later on, there are several sections which start with:

{{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\qj\fswiss MS Sans Serif;}{\f1\fdecor\fcharset2 Symbol;}{\f2\qj\fswiss\fprq2 Verdana;}{\f3\qj\fswiss MS Sans Serif;}{\f4\froman\fprq2\fcharset2 Symbol;}{\f5\qj\fswiss\fprq2\fcharset2 Symbol;}}

followed by text which contain references to these new font numbers.

So far, so good. But when later text has font references which refer back to the original {{\rtf1, the subsequent text is in the wrong font.

I tried merging a rich text box containing just the first {{rtf1 sequence ahead of each section of text. I also tried merging  a rich text box containing just the second {{rtf1 sequence ahead of each section of text. Neither approach fixed the fonts.

I really appreciate your help. I think that your routine will do the job if I can figure out how to apply it properly.
Ummm... I screwed up what I sent you... I don't quite know how I missed it... but here is a fix, I do believe:

In the MergeRTF function in the class, there is a line of code that says:

    'Remove Header information

There is then a for...next line:

    For lngRichTextBox = LBound(RichTextBoxes) To UBound(RichTextBoxes)

The next line is:

        lngInParen = 0

Right after that line, enter the following:

        blnCanQuit = False


This command allows the trimming of the headers on each of the text boxes.  This part was a late addition, and I didn't test it thoroughly.  Let me know if you have any other problems.  I kinda' like this little routine, and want it to work perfectly.
Avatar of mfrumer

ASKER

That addition changed the behavior, but now I find that my characters which were done in Symbol font (they follow the second {{\rtf1...) are no longer in that font. In the RTF file, the font coding for those characters has been removed.

The problem is probably with my coding since I have a rather strange situation.

I cannot build the entire rich text document in a rich text box because I am using RTF feautures (such as two-column and such) which are not supported by the rich text box control. So I build the RTF document in two stages.

First I build in a rich text box the parts of the document that the rich text box can handle. When I want to use an unsupported feature, I insert a "place holder".

I also build some sections as text strings and write them to files. These text strings contain rich text font definitions and uses. After Icreate the rich text file with its "place holders" , I run a non-VB program to edit the rich text file and replace the "place holders" with the separate files.

So, as I build the main RTF file and the separate "add in" files, I need to assure consistent font number usage.

It seems as if I am pretty close to getting it to work. It may just be that I have to merge pieces of rich text in a different order.

Avatar of mfrumer

ASKER

I am beginning to wonder whether RTF itself is working correctly.

The following is a piece of the output file. It looks to me as if "f0", which is NOT a symbol font should be used to print "SUPPLIES REQUIRED". Yet those words appear in a symbol font.

{{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\qj\fswiss MS Sans Serif;}{\f1\fdecor\fcharset2 Symbol;}{\f2\qj\fswiss\fprq2 Verdana;}{\f3\froman\fprq2\fcharset2 Symbol;}{\f4\qj\fswiss MS Sans Serif;}}
{\colortbl\red0\green0\blue0;}
\deflang1033\pard\li360\plain\f2\qj\fs20 Pressure meter or gauge (range 0 to 75 mm Hg; mercury manometers are not suitable)}
\cell \cell }\pard \widctlpar\intbl\adjustright {\row }\pard \widctlpar\adjustright {\par }
\plain\f0\qj\fs20
\par
\par \plain\f0\qj\fs20\b \trowd
\trgaph108\trleft-108\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \trbrdrh\brdrs\brdrw10 \trbrdrv\brdrs\brdrw10 \clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr
\brdrs\brdrw10 \clcbpat8\clshdng2000\cltxlrtb \cellx2880\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr\brdrs\brdrw10 \clcbpat8\cltxlrtb \cellx5310\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb
\brdrs\brdrw10 \clbrdrr\brdrs\brdrw10 \clcbpat8\clshdng2000\cltxlrtb \cellx8748\pard\plain \widctlpar\intbl\adjustright \qj\fs20\cgrid \cell \pard\plain \s1\qc\keepn\widctlpar\intbl\outlinelevel0\adjustright \b\qj\fs20\cgrid SUPPLIES REQUIRED\cell
\pard\plain \widctlpar\intbl\adjustright \qj\fs20\cgrid {\cell }\pard \widctlpar\intbl\adjustright {\row }\pard \widctlpar\adjustright \plain\f0\qj\fs20
\par
\par
\par {\sect }\sectd \sbknone\linex0\cols1\endnhere\sectdefaultcl \pard\plain \widctlpar\adjustright \qj\fs20\cgrid
\par \plain\f0
Avatar of mfrumer

ASKER

I did some more experimenting. Apparently, the existence of more than one {\fonttbl confuses an RTF reader such as Word or Word Viewer. It was the \f5 in the second {\fonttbl that was causing certain parts of the rest of my document to be in a Symbol font. Removing the \f5 reference to that {\fonttbl entry did not fix the problem. In fact, removing ALL references to ANY Symbol font did not help. But moving the \f5 font definition to the {\fonttbl in the document header and removing all other {\fonttbl sections fixed it.

So, since I have {\fonttbl sections in my "add in" RTF snippets, it looks like after I use your your routine in VB, my post processing routine must remove the extra {\fonttbl sections and figure out whether there are any fonts defined in them which must be moved to the single {\fonttbl at the start of the document.

Many thanks again for your excellent response.