View Single Post
  #6 (permalink)  
Old November 9th, 2004, 04:55 AM
jaucourt jaucourt is offline
Friend of Wrox
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts

Here is the code I've written and tested. If you create a new form and paste this code in, replacing all existing code.

To get this working for your own application may require a little bit of thought, but I've shown you all the principles behind what needs doing.

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        Me.Button2.Location = New System.Drawing.Point(104, 8)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Button2"
        Me.ListBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
        Me.ListBox1.Enabled = False
        Me.ListBox1.Location = New System.Drawing.Point(24, 48)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(128, 95)
        Me.ListBox1.TabIndex = 2
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(184, 157)
        Me.Name = "Form2"
        Me.Text = "Form2"

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add(New TestClass(True, "TestItem"))
    End Sub

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListBox1.Items.Add(New TestClass(False, "TestItem"))
    End Sub

    Private Sub OnDrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem
        Dim colourName As String

        ' need to have an Imports System.Drawing statement for this to work
        Dim rect As Rectangle = e.Bounds

        ' get the object we're dealing with...
        Dim myObj As Object = ListBox1.Items(e.Index)

        If (e.State And DrawItemState.Selected) Then
            ' fill rectangle with highlight colou...r
            e.Graphics.FillRectangle(SystemBrushes.Highlight, rect)
            ' not selected, just fill normally...
            e.Graphics.FillRectangle(SystemBrushes.Window, rect)
        End If

        ' get the colour of the item to be drawn...
        If myObj.Active Then colourName = "Red" Else colourName = "Green"
        ' build a brush of that colour
        Dim myBrush As New SolidBrush(Color.FromName(colourName))

        ' sort out our font - i.e. if it's an active item, make it bold...
        Dim myFont As Font

        If myObj.Active = True Then
            myFont = New Font(e.Font, FontStyle.Bold)
            myFont = e.Font.Clone
        End If

        ' draw the text in the correct colour; this also assumes that the property you want to show is called 'name'...
        e.Graphics.DrawString(myObj.Name.ToString, myFont, myBrush, rect.X + 1, rect.Y + 2)

        ' dispose of objects...

    End Sub

End Class

Class TestClass
    Public Active As Boolean
    Public Name As String

    Public Sub New()

    End Sub

    Public Sub New(ByVal Active As Boolean, ByVal Name As String)
        Me.Active = Active
        Me.Name = Name
    End Sub

End Class