How to Change the Text Color of ListBox Items in Visual Basic
- 1). Launch Visual Studio and create a new Visual Basic Windows Forms project. A new form named "Form1" opens in the editing window.
- 2). Click the "View" button, then click "Toolbox" to open Visual Studio's toolbox.
- 3). Double-click the "ListBox" control. Visual Studio places it on the form and names it "ListBox1."
- 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 color and size.
- 5). Click the "ForeColor" property. A drop-down arrow appears next to the property. Click that arrow to view a color menu containing tabs. The "System" tab displays Windows system colors; the "Web" tab displays the list of Web colors; and the "Custom" tab allows you to choose your own color from a color grid.
- 6). Click one of those tabs, then click the color you would like to use for the text of all ListBox items.
- 7). Click the "BackColor" item in the Properties window to display its drop-down arrow. Click that arrow to choose a color as described in the previous step. Visual Basic then uses the color you've selected as the background color for all ListBox items. Proceed to the next section if you want to change the color of only the selected ListBox item.
- 1). Double-click the form's title bar. The code for the form's "Load" method opens and displays an empty "Load" method.
- 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). 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
e.DrawBackground()
Dim brush As Brush = Brushes.Black
If (e.State And DrawItemState.Selected = DrawItemState.Selected) Then
brush = Brushes.Green
End If
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(),
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). Locate the statement that reads, "Dim brush As Brush = Brushes.Black." This line of code creates a "brush" variable that holds the color of the default drawing brush. In this instance, that color is "Black." Change "Black" to the color you would like to use to draw the nonselected listbox items. For instance, if you want to change the default item color to red, replace the previous statement with this:
Dim brush As Brush = Brushes.Red - 5). Locate the following statement in the code:
brush = Brushes.Green
Replace "Green" with any color. Visual Basic then uses that color to draw the ListBox's selected item. The final statement calls the "DrawString" method which draws the ListBox items. - 6). Press "F5" to run the program. The form appears and displays the ListBox and its items. Click any item to select it. Its color changes to the color you chose for the selected item. All other items display the default color.
Change Text Color of All Items
Change Text Color of a Selected Item
Source...