﻿'CREATED BY tassa FOR VBFORUMS.COM 
'5/August/2009
'IF YOU ARE GOING TO DISTRIBUTE,
'POST THIS CODE IN ANY OTHER ONLINE SERVICE,
'WEBSITE, ETC, GIVE THE NECESSARY CREDITS!
'Thanks :)

Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Public Class ColoredListBox
    Inherits Windows.Forms.ListBox

#Region " Declarations "
    Private _highlightedBorderColor, _highlightedFillColor, _
    _highlightedForeColor, _itemsBackColor, _itemsForeColor, _
    _itemsBorderColor As Color
#End Region

#Region " Subs "
    Public Sub New()
        Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
    End Sub
#End Region

#Region " Highlight Properties "

    ''' <summary>
    ''' The border color of the highlighted item(s).
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>

    <Category("Appearance")> _
    <Description("The border color of the highlighted item(s).")> _
    Public Property HighlightedBorder() As Color
        Get
            Return Me._highlightedBorderColor
        End Get
        Set(ByVal value As Color)
            Me._highlightedBorderColor = value
        End Set
    End Property

    ''' <summary>
    ''' The background color of the highlighted item(s).
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>

    <Category("Appearance")> _
    <Description("The background color of the highlighted item(s).")> _
    Public Property HighlightedFillColor() As Color
        Get
            Return Me._highlightedFillColor
        End Get
        Set(ByVal value As Color)
            Me._highlightedFillColor = value
        End Set
    End Property

    ''' <summary>
    ''' The forecolor of the highlighted item(s).
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>

    <Category("Appearance")> _
    <Description("The forecolor of the highlighted item(s).")> _
    Public Property HighlightedForeColor() As Color
        Get
            Return Me._highlightedForeColor
        End Get
        Set(ByVal value As Color)
            Me._highlightedForeColor = value
        End Set
    End Property
#End Region

#Region " Item Properties "

    ''' <summary>
    ''' The border color of the item(s).
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>

    <Category("Appearance")> _
    <Description("The border color of the item(s).")> _
    Public Property ItemsBorderColor() As Color
        Get
            Return Me._itemsBorderColor
        End Get
        Set(ByVal value As Color)
            _itemsBorderColor = value
        End Set
    End Property

    ''' <summary>
    ''' The background color of the item(s).
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>

    <Category("Appearance")> _
    <Description("The background color of the item(s).")> _
    Public Property ItemsBackColor() As Color
        Get
            Return Me._itemsBackColor
        End Get
        Set(ByVal value As Color)
            Me._itemsBackColor = value
        End Set
    End Property
#End Region

#Region " ColoredListBox Events "
    Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
        MyBase.OnDrawItem(e)
        Try
            e.DrawBackground()
            Dim myBrush As SolidBrush = Nothing
            Dim itemColor As SolidBrush = Nothing
            Dim myPen As Pen = Nothing
            If e.State = (DrawItemState.Selected Or DrawItemState.Focus) Then
                myBrush = New SolidBrush(_highlightedFillColor)
                myPen = New Pen(_highlightedBorderColor)
                itemColor = New SolidBrush(_highlightedForeColor)
            Else
                myBrush = New SolidBrush(_itemsBackColor)
                myPen = New Pen(_itemsBorderColor)
                itemColor = New SolidBrush(e.ForeColor)
            End If
            e.Graphics.FillRectangle(myBrush, e.Bounds)
            e.Graphics.DrawRectangle(myPen, e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height)
            e.Graphics.DrawString(Me.GetItemText(Me.Items(e.Index)), _
                            e.Font, itemColor, e.Bounds.X + 2, e.Bounds.Y, StringFormat.GenericDefault)
            myBrush.Dispose()
            itemColor.Dispose()
            myPen.Dispose()
        Catch ex As Exception

        End Try
    End Sub
#End Region

End Class
