VB3 Display Multiple Foreground Text Colors in VB List Box

ID: Q108811

3.00 WINDOWS kbprg

The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SUMMARY

To set the foreground and background color of a list box control, set the ForeColor and BackColor properties at either design time or run time. All text in a list box uses the color set by the ForeColor property. The text is printed against a background color set by the BackColor property.

Visual Basic doesn't directly support the display of text of different colors simultaneously in the list box. This article describes how to display words of different colors simultaneously in a list box by using an indirect technique.

MORE INFORMATION

You can display lines or words of different colors simultaneously in a list box by using one of the following indirect techniques.

1. Simulate the list box with a picture box control. You can store the

   desired text strings in an array of strings, and use the Print
   method to write the array entries into the picture box with different
   ForeColor properties. For example:

      picture1.BackColor = QBColor(14)  ' 14=Light yellow
      picture1.ForeColor = QBColor(4)   '  4=Red
      picture1.Print "in living red"
      picture1.ForeColor = QBColor(2)   '  2=Green
      picture1.Print "in living green"

   You can also add a vertical scroll bar next to the picture box. When the
   scroll bar is scrolled, your code needs to redraw the picture box. The
   ForeColor property of the picture box controls the current color used
   by the Print method. The picture box will not let you highlight text.
   NOTE: The BackColor method erases any pre-existing text on the picture
   control.

2. For coloring list box entries with multiple foreground colors, the
   Desaware company provides two solutions:

   a. The MLIST2.VBX control file is included with the Custom Control
      Factory product from Desaware. MLIST2.VBX allows each line in a list
      box to be colored independently. All words on the same line must be
      the same color. MLIST2.VBX comes with full source code.

   b. A more flexible and advanced solution is to turn Visual Basic's list
      box into an owner-draw list box. Desaware says that you can make true
      owner-draw list boxes with their SpyWorks-VB product. SpyWorks-VB
      allows you to color each entry of the list box with the full power of
      the Windows API drawing functions. SpyWorks-VB comes with sample
      source code for an owner-draw list box and command button, along with
      explanations of how to turn the standard controls into owner-draw
      controls. See the section on owner-draw controls further below.

How to Contact Desaware

NOTE: Desaware products are manufactured independent of Microsoft. Microsoft makes no warranty, implied or otherwise, regarding these products' performance or reliability.

Desaware 5 Town & Country Village #790 San Jose, CA 95128 Contact: Dan Appleman (408) 377-4770 Fax: (408) 371-3530

The Desaware company offers the following products:

1. Custom Control Factory -- an interactive development tool for creating

   custom controls including Animated Pushbuttons, Multistate Buttons,
   enhanced buttons, check box, and option button controls for Windows
   applications.

2. CCF-Cursors -- provides you with complete control over cursors (mouse
   pointers) in Visual Basic applications. Create your own cursors or
   convert icons to cursors, and much more. Includes over 50 cursors.

3. SpyWorks-VB -- an advanced development tool for use with Visual Basic.

Owner-Draw Controls in Windows

The owner-draw list capability is appropriate for advanced programmers for Microsoft Windows. You will need a good reference for the Windows API to learn the required drawing functions.

Owner-draw controls were introduced in Windows version 3.0. Because your application does all the drawing of the contents of the controls, you can customize them any way you like. Owner-draw controls are similar to predefined controls in that Windows will handle the control's functionality and mouse and keyboard input processing. However, you are responsible for drawing the owner-draw control in its normal, selected, and focus states.

You can create owner-draw controls from the menu, button, and list-box classes. You can create owner-draw combo boxes, but they must have the CBS_DROPDOWNLIST style, which equates to a static text item and a list box. The elements of an owner-draw control can be composed of strings, bitmaps, lines, rectangles, and other drawing functions in any combination, in your choice of colors.

REFERENCES

KBCategory: kbprg KBSubcategory: PrgCtrlsCus PrgCtrlsStd PrgCtrlsThird Additional reference words: 3.00
Keywords          : kb3rdparty PrgCtrlsCus PrgCtrlsStd PrgCtrlsThird 
Version           : 3.00
Platform          : WINDOWS

Last Reviewed: May 13, 1998