Results 1 to 1 of 1

Thread: Classic VB - How do I use multiple colours/fonts in a single textbox?

  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Classic VB - How do I use multiple colours/fonts in a single textbox?

    There is a simple answer to this, I'm afraid you basically can't - as VB's textbox allows only one colour and only one font.

    There is an easy alternative, which is also included in the controls available to you, the Rich Textbox (also known as RTB).

    This control will not usually be in the toolbox along with the normal textbox, so you will need to add it. To do this you need to do the following:
    • Go to the components screen (either right click the toolbox and click "Components", or go to "Project" -> "Components")
    • Scroll down the list until you see "Microsoft Rich Textbox Control 6.0" (the number may differ, and you may also have something like "(SP6)" at the end)
    • Tick the box next to it.
    • Press "OK".
    The control will now be added to the toolbox, and has an icon like this:


    In order to change the font or colour of part of the text you need to set the selection first (using the SelStart and SelLength properties), and then you can set various properties to change the colour and/or font.

    This example will set the text, change the colour of a couple of characters, and also make them bold:
    VB Code:
    1. With RichTextBox1
    2.   .Text = "Hello!"
    3.   .SelStart = 3
    4.   .SelLength = 2
    5.   .SelColor = vbRed
    6.   .SelBold = True
    7. End With
    There are several properties which you can set to change just part of the text, all of them start with "Sel".


    For more RichTextBox tips & tricks, see this CodeBank thread.
    Last edited by si_the_geek; Aug 8th, 2006 at 06:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width