Rich Textbox Bold Monospaced font 
Author Message
 Rich Textbox Bold Monospaced font

Hi,

I have a Rich Textbox that i need to have data line up in.  Everything is
great, except when I bold text, then it gets out of whack.  I am using font
Lucida Console, but also tried Andale.  When I cut the text from the RTB,
and put it in word, then it lines up nicely.  Is there any way to get a
monospaced font to line up with bold text in a Rich TextBox?

Alternatively, is there an ActiveX control for word that I can use on a
form?

Using VB6, Access 2000.



Mon, 06 Sep 2004 02:04:15 GMT  
 Rich Textbox Bold Monospaced font

Quote:
> Hi,

> I have a Rich Textbox that i need to have data line up in.  Everything is
> great, except when I bold text, then it gets out of whack.  I am using
font
> Lucida Console, but also tried Andale.  When I cut the text from the RTB,
> and put it in word, then it lines up nicely.  Is there any way to get a
> monospaced font to line up with bold text in a Rich TextBox?

Funny. I've never noticed that before and I have an interest in fonts that
is bordering on "unhealthy"!

I've just tried various applications and some seem to think that bold in a
monospaced font should be wider than normal (Microsoft Excel, Outlook
Express, Visual Basic Picture Boxes / RichText Boxes, MGI PhotoSuite)
whereas others (Microsoft Word, Microsoft Publisher, Corel Draw, Adobe
Photoshop 6 and my HP DeskJet printer) normally think that they should be
the same width. I say "normally", because most "intelligent" applications
display text to the user in the same way that it will appear when it is sent
to the currently selected output device, which is normally the default
printer.

The Visual Basic RichTextBox shows bold monospaced text as being wider than
normal monospaced text, regardless of the current default output device
(printer). This is something that I have only just discovered (because of
your posted question) and it is already beginning to annoy me! In fact, I
think I'll open another bottle of Budweiser before I continue . . .

. . . this raises some rather interesting little questions. For example,
place two identical RichTextBoxes on a Form and set them both to the Lucida
Console font. Set one to Normal and the other to Bold. Now set the Text
property of both RichTextBoxes to the same string ("This is a little test of
text width", for example). Drag out the size of the RichTextBoxes so that
the entire text fits onto one line. You will see that the text in the "bold"
RichTextBox is wider than the text in the "normal" one. Now print the
contents of both text boxes, using something like the following code:

Private Sub Command1_Click()
Printer.Print
RichTextBox1.SelPrint (Printer.hDC)
RichTextBox2.SelPrint (Printer.hDC)
End Sub

You should get two pages printed, and the "bold" text on one page will be
slightly wider than the "normal" text on the other page (although the actual
percentage difference in width will not be the same as the difference in the
displayed RichTextBoxes).

Now change the font in both RichTextBoxes to Courier, leaving one of them
set to normal and the other to bold as before. You will see that the text in
the "bold" RichTextBox is wider than the text in the "normal" one, just as
before. Now print the contents of both text boxes, using the same code. This
time, you will see that the text on the "normal" page" is exactly the same
width as the text on the "bold" page. Definitely not WYSIWYG!

The above happens, of course, because many printers have Courier as a "built
in" font, whereas very few will have Lucida Console "built in".

So, the answer to your question depends on what it is you wish to do with
the text in your RichTextBox. Do you want to simply lay it out so that
monospaced font characters always take up the same space on the monitor,
regardless of whether they are bold or normal, or do you want it to reflect
what is actually going to happen when you send it to the printer?

. . . or, alternatively, do you want to lay out your monospaced font
characters so that they always take up the same space (bold or not) on both
the display and the printer, irrespective of whether or not the currently
attached printer has that specific font installed?

You can write code to do any (or all) of the above - but not in a
RichTextBox! My advice would be to use a VB Picture Box to display your
text, because this will enable you to write code that specified the exact
position of each and every character. You can use the same code to send the
characters to the printer, so that your printed output always reflects your
displayed output, regardless of the font used ot the fonts that may (or may
not) be installed in the printer.

This is the sort of thing that Microsoft Word does. It "asks" Windows what
the text will look like on the currently selected printer for that document
and it then "draws" the text into the display so that it matches the
expected output. I'm not sure, but I think there is actually an API that
does that sort of thing for you (something like "draw some text into one
specified device but use the output specification of another specified
device when you do so"). If not, there should be!

Anyway, as I have suggested, you can get excellent results by writing a few
lines of code to "draw" a text string into an output device by looping
through the string and setting the exact position of each character using
the CurrentX property. This will easily enable you to draw the text (into a
Picture Box or the Printer object) so that each character has the same width
irrespective of whether it is bold or not.

Sorry I can't be of more help, but it is difficult without knowing your
*exact* requirements.

Mike



Mon, 06 Sep 2004 10:54:43 GMT  
 Rich Textbox Bold Monospaced font
Thank you for that tremendous reply.  You did more testing than I did!

WHat I have is simple form, to display genetic data.  It is typical to
display this like
(not sure how this will display for you...)
  21451 tcccacgacc actcagccac ccctgcacac tctggcccac ccttgtgccc 21500
  21501 cggcccctct aggccccgcc tcctcccgcc cctgccccgc ccctttggct 21550

I have the data in a db, unformatted.  I retrieve it, put it in a hidden
textbox, then format the text and put it in the rtb.  Part of this is to
format the interesting bits, some in bold, some underlined, some colored, or
other combinations.  I can get away with bolding one character in a line,
but putting two or more in succession starts things going haywire.

So, you are saying to try a picture box?  It seem like way overkill for what
I need to do, is this easy, or not?  I think ideally, I want to do as you
said

<snip>lay out your monospaced font characters so that they always take up
the same space (bold or not) on both the display and the printer,
irrespective of whether or not the currently attached printer has that
specific font installed?
<snip>

In this less ideal world that I live in, just not using bold might be ok,
though not preferable.  Is there a place to go to see an example of using a
picture box?  It seems like it might be slow, I have text up to 32KB (length
of the memo field).

Thanks!


Quote:


> > Hi,

> > I have a Rich Textbox that i need to have data line up in.  Everything
is
> > great, except when I bold text, then it gets out of whack.  I am using
> font
> > Lucida Console, but also tried Andale.  When I cut the text from the
RTB,
> > and put it in word, then it lines up nicely.  Is there any way to get a
> > monospaced font to line up with bold text in a Rich TextBox?

> Funny. I've never noticed that before and I have an interest in fonts that
> is bordering on "unhealthy"!

> I've just tried various applications and some seem to think that bold in a
> monospaced font should be wider than normal (Microsoft Excel, Outlook
> Express, Visual Basic Picture Boxes / RichText Boxes, MGI PhotoSuite)
> whereas others (Microsoft Word, Microsoft Publisher, Corel Draw, Adobe
> Photoshop 6 and my HP DeskJet printer) normally think that they should be
> the same width. I say "normally", because most "intelligent" applications
> display text to the user in the same way that it will appear when it is
sent
> to the currently selected output device, which is normally the default
> printer.

> The Visual Basic RichTextBox shows bold monospaced text as being wider
than
> normal monospaced text, regardless of the current default output device
> (printer). This is something that I have only just discovered (because of
> your posted question) and it is already beginning to annoy me! In fact, I
> think I'll open another bottle of Budweiser before I continue . . .

> . . . this raises some rather interesting little questions. For example,
> place two identical RichTextBoxes on a Form and set them both to the
Lucida
> Console font. Set one to Normal and the other to Bold. Now set the Text
> property of both RichTextBoxes to the same string ("This is a little test
of
> text width", for example). Drag out the size of the RichTextBoxes so that
> the entire text fits onto one line. You will see that the text in the
"bold"
> RichTextBox is wider than the text in the "normal" one. Now print the
> contents of both text boxes, using something like the following code:

> Private Sub Command1_Click()
> Printer.Print
> RichTextBox1.SelPrint (Printer.hDC)
> RichTextBox2.SelPrint (Printer.hDC)
> End Sub

> You should get two pages printed, and the "bold" text on one page will be
> slightly wider than the "normal" text on the other page (although the
actual
> percentage difference in width will not be the same as the difference in
the
> displayed RichTextBoxes).

> Now change the font in both RichTextBoxes to Courier, leaving one of them
> set to normal and the other to bold as before. You will see that the text
in
> the "bold" RichTextBox is wider than the text in the "normal" one, just as
> before. Now print the contents of both text boxes, using the same code.
This
> time, you will see that the text on the "normal" page" is exactly the same
> width as the text on the "bold" page. Definitely not WYSIWYG!

> The above happens, of course, because many printers have Courier as a
"built
> in" font, whereas very few will have Lucida Console "built in".

> So, the answer to your question depends on what it is you wish to do with
> the text in your RichTextBox. Do you want to simply lay it out so that
> monospaced font characters always take up the same space on the monitor,
> regardless of whether they are bold or normal, or do you want it to
reflect
> what is actually going to happen when you send it to the printer?

> . . . or, alternatively, do you want to lay out your monospaced font
> characters so that they always take up the same space (bold or not) on
both
> the display and the printer, irrespective of whether or not the currently
> attached printer has that specific font installed?

> You can write code to do any (or all) of the above - but not in a
> RichTextBox! My advice would be to use a VB Picture Box to display your
> text, because this will enable you to write code that specified the exact
> position of each and every character. You can use the same code to send
the
> characters to the printer, so that your printed output always reflects
your
> displayed output, regardless of the font used ot the fonts that may (or
may
> not) be installed in the printer.

> This is the sort of thing that Microsoft Word does. It "asks" Windows what
> the text will look like on the currently selected printer for that
document
> and it then "draws" the text into the display so that it matches the
> expected output. I'm not sure, but I think there is actually an API that
> does that sort of thing for you (something like "draw some text into one
> specified device but use the output specification of another specified
> device when you do so"). If not, there should be!

> Anyway, as I have suggested, you can get excellent results by writing a
few
> lines of code to "draw" a text string into an output device by looping
> through the string and setting the exact position of each character using
> the CurrentX property. This will easily enable you to draw the text (into
a
> Picture Box or the Printer object) so that each character has the same
width
> irrespective of whether it is bold or not.

> Sorry I can't be of more help, but it is difficult without knowing your
> *exact* requirements.

> Mike



Mon, 06 Sep 2004 23:01:53 GMT  
 Rich Textbox Bold Monospaced font
Hi Mathieu

Using a Picture Box is not as difficult as you think, and it gives you total
and absolute control, which is something you do not get from Text Boxes or
RichText Boxes. The Picture box allows you to write or print or draw
anything you want into it, at any desired position. This means that, if you
take a little time to write your code correctly, there is *nothing* that you
cannot do!

You have said that you are using a data file that is up to 32K in length,
and from your description of your data it would appear that each individual
line of text is about 60 or so characters long. That implies a data height
(for want of a better word) of about 500 lines. That amount of "visible"
data can usually be contained in a single Picture Box. For example, if you
are using the Lucida Console font (as you say you are) then the width of a
suitable Picture Box for a font size of 8 points bold would be about 600
pixels by 5500 pixels (at the standard Windows "small fonts" setting). This
is well within the range of size of a VB Picture Box, so you could easily
print your entire data block into one "long" VB Picture Box. You could place
that Picture Box into another Picxture Box (one that is entirely visible on
the screen) in such a way that the "data" picture box is "contained" within
the "displayed" picture box. Then you could place a vertical scroll bar
against the pair so that the user would be able to "scroll up and down" to
viuew the data (your code would need to set the "Top" property of the
"contained" picture box in accordance with the user's setting of the scroll
bar.

The advantage with this method is that you can do just about anything you
want. For example, you can have most of the text in standard ("not bold")
font and you can have any one or more characters in "bold". You can also
colour each character individually, in whatever way you wish. You can draw
little circles or ovals around certain characters or groups of characters,
and you can even have little "animated sprites" that "dance around" specific
parts of your data to draw the user's attention to them! The world is your
oyster (if you happen to like oysters!).

The only problem with this specific method is that it requires an "internal"
picture box that is large enough to hold the entire data set, and there are
limitations. The first limitation is that you cannot have a Picture Box that
is higher (or wider) than 16383 pixels (about 170 inches at the standard "96
pixels per inch" Small Fonts setting). This limitation won't be a problem in
your particular case because there is a maximum of 500 "lines" of data, and
you would need to be using a very large font size to exceed that figure.

The major limitation (one that you might well come across) is that in order
for our "little trick" to work we need to use an Autoredraw = True picture
box to contain our data. The problem here is that an Autoredraw picture box
requires a contiguous block of "real" memory (*not* Windows virtual memory).
In your specific case you would need a Picture Box that has about 10
Megabytes of "real" memory. This may not sound a lot in this "enlightened"
age, but Windows "eats memnory for breakfast", and it is quite likely that a
contiguous block of that size may not be available even on systems that have
128 Mb of "real" memory. For "lesser" system there is even more of a
problem.

You shouldn't worry about that too much, though, because it is possible to
write your stuff in such a way that you use a Picture Box that is actually
smaller than the display and you write your code in such a way that only the
"currently visible" lines of text are displayed. This requires more worik,
and is slower for the user, but it works okay.

Anyway, back to your current problem. Suppose you have a data set of up to
500 lines, with each line containing up to 60 or 70 characters (as it would
appear from your initial explanation). You could simply write the entire
data set into a VB picture box (using the VB Print command)  and you could
then "scroll" that picture box within a "container" picture box, so that the
user could use a vertical scroll bar to enable him to see all of the data.
That is the simplest method, and it would enable you to do all sorts of
things with the display.

Have a look at the following. It runs slowly (about one second to print the
entire data set into the Picture Box) simply because it prints theb data
"one character at a time". There are faster (and more "code intensive") ways
to do this, but maybe a one second delay will be acceptable to you. Let me
know what you think:

Mike

Option Explicit

Private Sub Command1_Click()
Dim t1 As Single, t2 As Single
Dim n As Long, j As Long, h As Single, w As Single
Dim xpos As Single
Dim r1 As Single, r2 As Single, r3 As Single
Dim mydata(1 To 500) As String
Dim stringdata(1 To 5) As String
stringdata(1) = "21451 tcccacgacc actcagccac ccctgcacac tctggcccac
ccttgtgccc 21500"
stringdata(2) = "21501 gatcccctct aggccccgcc tcctcccgcc cctgccccgc
ccctttggct 21550"
stringdata(3) = "21678 mikeccctct aggccccgcc tcctcccgcc cctgccccgc
ccctttggct 21550"
stringdata(4) = "23456 whiskyccct aggccccgcc tcctcccgcc cctgccccgc
ccctttggct 21550"
stringdata(5) = "29897 cokeccctct aggccccgcc tcctcccgcc cctgccccgc
ccctttggct 21550"
Randomize
For n = 1 To 500
  mydata(n) = stringdata(Rnd * 4 + 1)
  Next n
Close 1
Me.ScaleMode = vbPixels
With Picture1
  .ScaleMode = vbPixels
  .BorderStyle = 0
  .BackColor = vbWhite
  .AutoRedraw = True
  .FontName = "Lucida Console"
  .FontSize = 8
  .FontBold = True
  w = Picture1.TextWidth("H")
  h = Picture1.TextHeight("H")
  .Height = h * 300 ' enough for 500 lines
  .Width = w * 68 ' of up to 68 characters
  .Cls
End With
On Error GoTo NotEnoughRealMemory
  Picture1.PSet (0, 0), vbWhite
On Error GoTo 0
t1 = Timer
Picture1.FontBold = False
Picture1.FontTransparent = False
For n = 1 To 500
  xpos = 10 ' a bit of left margin
  r1 = Rnd
  r2 = Rnd
  If r1 > 0.8 Then
    Picture1.FontBold = True
    Picture1.ForeColor = vbBlue
  Else
    Picture1.FontBold = False
    Picture1.ForeColor = vbBlack
  End If
  If r2 > 0.8 Then
    Picture1.FontBold = True
  Else
    Picture1.FontBold = False
  End If
  For j = 1 To Len(mydata(n))
    Picture1.CurrentX = xpos
    Picture1.Print Mid$(mydata(n), j, 1);
    xpos = xpos + w
  Next j
  r3 = Rnd
  If r3 > 0.8 Then
    Picture1.FontItalic = True
  Else
    Picture1.FontItalic = False
  End If
  Picture1.CurrentY = Picture1.CurrentY + h
Next n
t2 = Timer
Caption = t2 - t1 & "  " & Format(Picture1.Width)
Caption = Picture1.Width & "  " & Picture1.Height
Exit Sub
NotEnoughRealMemory:
' here you would write code to do the job a different
' way (printing only those lines that are visible)
' which is slower to update as the user scrolls but
' which doesn't have a memory problem.
MsgBox "Get some more real memory on this {*filter*}y box!"
Exit Sub
End Sub


Quote:
> Thank you for that tremendous reply.  You did more testing than I did!

> WHat I have is simple form, to display genetic data.  It is typical to
> display this like
> (not sure how this will display for you...)
>   21451 tcccacgacc actcagccac ccctgcacac tctggcccac ccttgtgccc 21500
>   21501 cggcccctct aggccccgcc tcctcccgcc cctgccccgc ccctttggct 21550

> I have the data in a db, unformatted.  I retrieve it, put it in a hidden
> textbox, then format the text and put it in the rtb.  Part of this is to
> format the interesting bits, some in bold, some underlined, some colored,
or
> other combinations.  I can get away with bolding one character in a line,
> but putting two or more in succession starts things going haywire.

> So, you are saying to try a picture box?  It seem like way overkill for
what
> I need to do, is this easy, or not?  I think ideally, I want to do as you
> said

> <snip>lay out your monospaced font characters so that they always take up
> the same space (bold or not) on both the display and the printer,
> irrespective of whether or not the currently attached printer has that
> specific font installed?
> <snip>

> In this less ideal world that I live in, just not using bold might be ok,
> though not preferable.  Is there a place to go to see an example of using
a
> picture box?  It seems like it might be slow, I have text up to 32KB
(length
> of the memo field).

> Thanks!





> > > Hi,

> > > I have a Rich Textbox that i need to have data line up in.  Everything
> is
> > > great, except when I bold text, then it gets out of whack.  I am using
> > font
> > > Lucida Console, but also tried Andale.  When I cut the text from the
> RTB,
> > > and put it in word, then it lines up nicely.  Is there any way to get
a
> > > monospaced font to line up with bold text in a Rich TextBox?

> > Funny. I've never noticed that before and I have an interest in fonts
that
> > is bordering on "unhealthy"!

> > I've just tried various applications and some seem to think that bold in
a
> > monospaced font should be wider than normal (Microsoft Excel, Outlook
> > Express, Visual Basic Picture Boxes / RichText Boxes, MGI PhotoSuite)
> > whereas others (Microsoft Word, Microsoft Publisher, Corel Draw, Adobe
> > Photoshop 6 and my HP DeskJet printer) normally think that they should
be
> > the same width. I say "normally", because most "intelligent"
applications
> > display text to the user in the same way that it will appear when it is
> sent
> > to the currently selected output device, which is normally the default
> > printer.

> > The Visual Basic RichTextBox shows bold monospaced text as being wider
> than
> > normal monospaced text, regardless of the current default output device
> > (printer). This is something

...

read more »



Tue, 07 Sep 2004 10:03:15 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. RichTextBox Bold Monospaced Fonts

2. Strange font behaviour of rich textbox. (VB6)

3. Symbol fonts in Rich Textboxes

4. Adding to Rich Text in Rich Textbox

5. Textbox as a rich-textbox?

6. Textbox as a rich-textbox?

7. Rich Textbox / Textbox Scrolling

8. Rich Text Control and searching for bolded text

9. Change font and font color inside a textbox

10. Bold Fonts in a SendObject command

11. VBA to change Footer Format - Fonts, Size, Bold etc

12. Bold & other font attributes

 

 
Powered by phpBB® Forum Software