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

Updated April 17, 2017

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.

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

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

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

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.

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.

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

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.

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

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.

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.

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

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.

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.


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.

Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

After majoring in physics, Kevin Lee began writing professionally in 1989 when, as a software developer, he also created technical articles for the Johnson Space Center. Today this urban Texas cowboy continues to crank out high-quality software as well as non-technical articles covering a multitude of diverse topics ranging from gaming to current affairs.