This is extremely similar to my ColorComboBox, except I have it hold a separate list for the colors to be displayed and instead of coloring the text itself, draws a box with the color in it.
I'm glad I'm not the only one who had a use for something like this.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
I see, it's a very cool class that you came up with. It hadn't been my intention to post something similar, I just thought that someone would like to have a colored listbox, with a different highlight and the text being, ummm, "unique". I used the names of the colors as an example string since I didn't know what to write. Lol...
"In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
Niklaus E. Wirth
Rate any post that helped you, it's a good way of saying thanks
Please specify your Visual Studio Version!
Just to let you know, before you zip your project and upload it to the site here, go in and delete the Bin and Obj folders because they contain compiled code which isn't allowed on this forum.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
Option Explicit On
Option Strict On
Option Infer Off
Imports System.ComponentModel
Public Class ColoredListBox
Inherits System.Windows.Forms.ListBox
Public Sub New()
MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
Me.Items.Clear()
End Sub
<Browsable(False), EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), _
DefaultValue(Windows.Forms.DrawMode.OwnerDrawVariable)> _
Public Shadows ReadOnly Property DrawMode() As System.Windows.Forms.DrawMode
Get
Return System.Windows.Forms.DrawMode.OwnerDrawVariable
End Get
End Property
Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)
Select Case True
Case Me.Font.SizeInPoints > 12.0F : Me.Font = New Font(Me.Font.FontFamily, 12.0F, Me.Font.Style)
Case Me.Font.SizeInPoints < 8.0F : Me.Font = New Font(Me.Font.FontFamily, 8.0F, Me.Font.Style)
End Select
End Sub
Protected Overrides Sub onDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
e.DrawBackground()
If e.Index >= 0I AndAlso DesignMode = False Then
Dim myBrush As Brush = Brushes.White
Dim myPen As Pen = Pens.Blue
If e.State = (DrawItemState.Selected Or DrawItemState.Focus) Then
myBrush = Brushes.Black
myPen = Pens.Red
End If
e.Graphics.FillRectangle(myBrush, e.Bounds)
e.Graphics.DrawRectangle(myPen, e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1I, e.Bounds.Height)
e.Graphics.DrawString(CType(Me.Items(e.Index), ItemDate).ToString, Me.Font, New SolidBrush(CType(Me.Items(e.Index), ItemData).ItemColor), e.Bounds.X + 2, e.Bounds.Y, StringFormat.GenericDefault)
End If
End Sub
End Class
Public Class ItemData
Private m_Item As Object
Private m_Color As Color
Public Sub New(ByVal Item As Object, ByVal ItemColor As Color)
m_Item = Item
m_Color = ItemColor
End Sub
Public Property Item() As Object
Get
Return m_Item
End Get
Set(ByVal value As Object)
m_Item = value
End Set
End Property
Public Property ItemColor() As Color
Get
Return m_Color
End Get
Set(ByVal value As Color)
m_Color = value
End Set
End Property
Public Overrides Function ToString() As String
Return m_Item.ToString()
End Function
End Class
Last edited by JuggaloBrotha; Aug 1st, 2009 at 08:24 AM.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.