How to change the text color of listbox items in visual basic

Written by kevin lee
  • Share
  • Tweet
  • Share
  • Email

Visual Basic's "DrawItem" method allows you to build imaginative controls. The ListBox control, for example, displays items in a list; however, you can only change the text colour of every item in the control. To create an enhanced ListBox that displays a selected item's text in a different colour, you have to make a small change to the ListBox's "DrawItem" method.

Skill level:
Moderately Easy

Other People Are Reading


    Change Text Color of All Items

  1. 1

    Launch Visual Studio and create a new Visual Basic Windows Forms project. A new form named "Form1" opens in the editing window.

  2. 2

    Click the "View" button, then click "Toolbox" to open Visual Studio's toolbox.

  3. 3

    Double-click the "ListBox" control. Visual Studio places it on the form and names it "ListBox1."

  4. 4

    Click the "ListBox1" control and press "F4" to view the Properties window. This window allows you to set a control's properties, such as colour and size.

  5. 5

    Click the "ForeColor" property. A drop-down arrow appears next to the property. Click that arrow to view a colour menu containing tabs. The "System" tab displays Windows system colours; the "Web" tab displays the list of Web colours; and the "Custom" tab allows you to choose your own colour from a colour grid.

  6. 6

    Click one of those tabs, then click the colour you would like to use for the text of all ListBox items.

  7. 7

    Click the "BackColor" item in the Properties window to display its drop-down arrow. Click that arrow to choose a colour as described in the previous step. Visual Basic then uses the colour you've selected as the background colour for all ListBox items. Proceed to the next section if you want to change the colour of only the selected ListBox item.

    Change Text Color of a Selected Item

  1. 1

    Double-click the form's title bar. The code for the form's "Load" method opens and displays an empty "Load" method.

  2. 2

    Paste the following code before that method's "End Sub" statement:

    ListBox1.Items.AddRange(New Object() {"Item 1", "Item", "Item", "Item 4"})

    ListBox1.DrawMode = DrawMode.OwnerDrawFixed

    The first statement adds items to the ListBox. The next statement sets the control's drawing mode to "OwnerDrawFixed." Use this mode when you wish to override Visual Basic's default drawing mode for a control.

  3. 3

    Paste the following code after the "Load" method's "End Sub" statement:

    Private Sub ListBox1_DrawItem(ByVal sender As Object, _

    ByVal e As System.Windows.Forms.DrawItemEventArgs) _

    Handles ListBox1.DrawItem


    Dim brush As Brush = Brushes.Black

    If (e.State And DrawItemState.Selected = DrawItemState.Selected) Then

    brush = Brushes.Green

    End If


    e.Font, brush, _

    e.Bounds, StringFormat.GenericDefault)

    End Sub

    This code overrides the ListBox's "DrawItem" method. The "DrawItem" method runs every time Visual Basic draws a ListBox item on the control.

  4. 4

    Locate the statement that reads, "Dim brush As Brush = Brushes.Black." This line of code creates a "brush" variable that holds the colour of the default drawing brush. In this instance, that colour is "Black." Change "Black" to the colour you would like to use to draw the nonselected listbox items. For instance, if you want to change the default item colour to red, replace the previous statement with this:

    Dim brush As Brush = Brushes.Red

  5. 5

    Locate the following statement in the code:

    brush = Brushes.Green

    Replace "Green" with any colour. Visual Basic then uses that colour to draw the ListBox's selected item. The final statement calls the "Drawstring" method which draws the ListBox items.

  6. 6

    Press "F5" to run the program. The form appears and displays the ListBox and its items. Click any item to select it. Its colour changes to the colour you chose for the selected item. All other items display the default colour.

Tips and warnings

  • Use Visual Studio's "Intellisense" feature to help you choose colour names when adding them to the "DrawItem" method. Consider this statement in that method: "Dim brush As Brush = Brushes.Green." When you type the period after "Brushes.," a menu appears and displays all available colours. Tab to the one you wish to use and press "Enter" to select it. Visual Basic now updates the line of code for you.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.