none
Question on ListBox items

    Question

  • Hi,

    How to display items in a ListBox with different font style (e.g. Bold,Italic) and different color for different items?Example below is a ListBox that contains:

    Visual Studio
    Visual Basic
    Visual C++
    C#
    Java
    HTML

    Can this be done in vb.net?

    Tuesday, November 27, 2007 11:34 AM

Answers

  • Here's a quick sample using OwnerDrawFixed mode.  There are many way's to do this depending on your problem but this is a fairly easy quick way.

     

    Public Class Form1

    Private list As New List(Of Info)

    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

    e.DrawBackground()

    e.DrawFocusRectangle()

    Dim br As New SolidBrush(list(e.Index)._color)

    e.Graphics.DrawString(list(e.Index)._item, list(e.Index)._font, br, e.Bounds.Left, e.Bounds.Top)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ListBox1.DrawMode = DrawMode.OwnerDrawFixed

    ListBox1.Font = New Font("Arial", 8.25, FontStyle.Regular, GraphicsUnit.Point, Nothing)

    list.Add(New Info("Visual Studio", Color.Black, New Font("Arial", 8.25, FontStyle.Bold, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("Visual Basic", Color.Black, New Font("Arial", 8.25, FontStyle.Italic, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("Visual C++", Color.Red, New Font("Arial", 8.25, FontStyle.Italic, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("C#", Color.Black, New Font("Arial", 8.25, FontStyle.Regular, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("Java", Color.Black, New Font("Arial", 8.25, FontStyle.Bold, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("HTML", Color.Black, New Font("Arial", 8.25, FontStyle.Bold, GraphicsUnit.Point, Nothing)))

    For Each item As Info In list

    ListBox1.Items.Add(item._item)

    Next

    End Sub

    End Class

    Public Class Info

    Public _item As String

    Public _color As New Color

    Public _font As Font

    Sub New(ByVal value As String, ByVal color As Color, ByVal font As Font)

    _item = value

    _color = color

    _font = font

    End Sub

    End Class

    Tuesday, November 27, 2007 4:51 PM
  • I should also mention this was for Windows Forms.  If you're building a web app, that's different and you'd need to either purchase a control or write a custom control to emit your HTML.

    Tuesday, November 27, 2007 5:33 PM

All replies

  • Here's a quick sample using OwnerDrawFixed mode.  There are many way's to do this depending on your problem but this is a fairly easy quick way.

     

    Public Class Form1

    Private list As New List(Of Info)

    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

    e.DrawBackground()

    e.DrawFocusRectangle()

    Dim br As New SolidBrush(list(e.Index)._color)

    e.Graphics.DrawString(list(e.Index)._item, list(e.Index)._font, br, e.Bounds.Left, e.Bounds.Top)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ListBox1.DrawMode = DrawMode.OwnerDrawFixed

    ListBox1.Font = New Font("Arial", 8.25, FontStyle.Regular, GraphicsUnit.Point, Nothing)

    list.Add(New Info("Visual Studio", Color.Black, New Font("Arial", 8.25, FontStyle.Bold, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("Visual Basic", Color.Black, New Font("Arial", 8.25, FontStyle.Italic, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("Visual C++", Color.Red, New Font("Arial", 8.25, FontStyle.Italic, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("C#", Color.Black, New Font("Arial", 8.25, FontStyle.Regular, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("Java", Color.Black, New Font("Arial", 8.25, FontStyle.Bold, GraphicsUnit.Point, Nothing)))

    list.Add(New Info("HTML", Color.Black, New Font("Arial", 8.25, FontStyle.Bold, GraphicsUnit.Point, Nothing)))

    For Each item As Info In list

    ListBox1.Items.Add(item._item)

    Next

    End Sub

    End Class

    Public Class Info

    Public _item As String

    Public _color As New Color

    Public _font As Font

    Sub New(ByVal value As String, ByVal color As Color, ByVal font As Font)

    _item = value

    _color = color

    _font = font

    End Sub

    End Class

    Tuesday, November 27, 2007 4:51 PM
  • When I paste the code, the term 'List(Of Info)' is underlined with blue color saying that 'type 'List' is not defined'. Then I try to change it to ListBox and the term 'Info' is underlined with blue color line saying that 'Systems.Windows.Forms.ListBox has no type parameters and so cannot have type arguments'. How come? By the way I'm using vb.net 2005.
    Tuesday, November 27, 2007 5:29 PM
  •  

    For the list you may need to include

    Imports System.Collections.Generic

     

     

    Tuesday, November 27, 2007 5:32 PM
  • I should also mention this was for Windows Forms.  If you're building a web app, that's different and you'd need to either purchase a control or write a custom control to emit your HTML.

    Tuesday, November 27, 2007 5:33 PM
  • Let say if I would like to make it where whenever I click or double click on the selected item in a list, the font will change into red color, how can I do this?
    Sunday, February 03, 2008 7:34 AM
  • Depends on what you mean.  Below are some options.

    Private _lastClicked As Integer = -1

    Private _lastDoubleClicked As Integer = -1

    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click

    _lastClicked = ListBox1.SelectedIndex

    End Sub

    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick

    _lastDoubleClicked = ListBox1.SelectedIndex

    End Sub

    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

    If ListBox1.GetSelected(e.Index) Then

    ' Draw Selected

    ElseIf e.Index = _lastClicked Then

    ' Draw Last Clicked

    ElseIf e.Index = _lastDoubleClicked Then

    ' Draw last double clicked

    Else

    ' Draw regular item

    End If

    End Sub

     

     

    Note that if you want to track all the items that are clicked/double-clicked you would need a collection such as:

    Private _lastClicked As List(Of Integer)

     

    If you allow clicking/double clicking you should also think of a keyboard alternative (such as pressing the enter key or space bar).

     

    Hope that helps.

    Monday, February 04, 2008 7:45 PM
  • hi, I want to ask how to enable the horizontal scroll appear in a listbox? the item displayed in the lisbox is too long but I can't scroll to the right side.

    Tuesday, February 19, 2008 7:08 PM