Is there a way to make a item in listbox different in apperance than the rest?

  • Thread starter Thread starter Hugh
  • Start date Start date
Hi,

You need to make your listbox ownerdrawn. Here is a quick example
which loads all the font names in a listbox and draws the names in the font.

ListBox1.DrawMode = DrawMode.OwnerDrawFixed

Dim ff As FontFamily

For Each ff In FontFamily.Families

ListBox1.Items.Add(ff.Name)

Next



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

Dim g As Graphics = e.Graphics

Dim s As String

Dim d As Date

Dim br As Brush = SystemBrushes.WindowText

Dim brBack As Brush

Dim rDraw As Rectangle

Dim bSelected As Boolean = CBool(e.State And DrawItemState.Selected)

rDraw = e.Bounds

rDraw.Inflate(-1, -1)

If bSelected Then

brBack = Brushes.LightBlue

g.FillRectangle(Brushes.LightBlue, rDraw)

g.DrawRectangle(Pens.Blue, rDraw)

Else

brBack = Brushes.White

g.FillRectangle(brBack, e.Bounds)

End If

br = Nothing

brBack = Nothing

rDraw = Nothing

'

' Create a the font we are displaying

'

Dim fnt As Font = Me.Font

Try

s = cbo.Items.Item(e.Index).ToString

fnt = New Font(New FontFamily(s), Me.Font.Size, FontStyle.Regular)

Catch

s = ""

End Try

Dim y As Integer

y = e.Bounds.Top + 1

'

' Draw the font name in its font

'

g.DrawString(s, fnt, Brushes.Black, 2, y)

End Sub



Ken
 
Hi,

Sorry I converted the code from a combobox and forgot to change that
to listbox1.

Ken
-------------
 
Back
Top