p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB.NET 2002/2003 Basics (http://p2p.wrox.com/forumdisplay.php?f=76)
-   -   Change color of text in a listbox? (http://p2p.wrox.com/showthread.php?t=20696)

ja8261 November 1st, 2004 04:47 PM

Change color of text in a listbox?
 
How do you change the color of text in a listbox based upon a certain criteria? For example, as my code executes it will display either "Active" or "Inactive" in the listbox. I want "Active" to be a bold green and "Inactive" to be a bold red. Is there a simple way to do this? The "Active" and "Inactive" will be displayed in the listbox at the same time so I don't want to just change the ForeColors.

I was heading towards the idea of creating a simple function that will return "Active" in bold green or "Inactive" in bold red based upon a test condition I create.

Public Function ColoredText()
     If status = "Active" then
          ' code to create a variable that would store "Active" as bold green text
     Else
          ' code to create a variable that would store "Inactive" as bold red text
     end if
end function


Jeff Armstrong
Sr. Systems Mgr
SBC Services, Inc.

jaucourt November 4th, 2004 05:21 AM

Your best bet, I think, is to change the DrawMode of the listbox to OwnerDrawFixed. From there, you create your method for filling the listbox with items:

Code:

Private Sub FillListBox()
    ListBox1.ItemHeight = 24

    ' avoid flickering
    ListBox1.BeginUpdate
    ListBox1.Items.Clear()

    ' fill the list box with ListBox1.Items.Add(item)...
    For each item in collection
        ListBox1.Items.Add(item)
    Next

    ' Finish the update
    ListBox1.EndUpdate()

End Sub

From there, you need to trap the listbox DrawItem event and draw
each item yourself...
Code:

Private Sub OnDrawItem(sender As Object, e as DrawItemEventArgs) Handles ListBox1.DrawItem

    ' 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)
    Else
        ' 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)
    Else
        myFont = e.Font
    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, myFont, myBrush, rect.X + 4, rect.Y + 2)

    ' dispose of objects...
    myBrush.Dispose
    myFont.Dispose

End Sub

[Acknowledgement to Francesco Balena's book 'Programming Microsoft Visual Basic .Net' for bits of this answer!]


ja8261 November 4th, 2004 05:23 PM

I'm trying to implement your code but I am getting the error: Name 'colourName' is not declared. I get the impression that the colourName stores "Red" or "Green". I've tried declaring it but when I run the code, nothing happens. Any suggestions?

Jeff Armstrong
Sr. Systems Mgr
SBC Services, Inc.

jaucourt November 5th, 2004 05:08 AM

Right, two cock-ups on my part:

1) You're right, I forgot to declare colourName as a string at the top of the OnDrawItem method

2) Change the myFont = e.Font to myFont = e.Font.Clone, so when we dispose of myFont we don't dispose of ListBox1's own font object and thus make it unavailable for future use.

Code:

    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)
        Else
            ' 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)
        Else
            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...
        myBrush.Dispose()
        myFont.Dispose()

    End Sub


ja8261 November 8th, 2004 05:54 PM

The code builds with no errors, however I now get the error at the line: Dim myObj as Object = ListBox1.Items(e.Index). The error message is:

"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll

Additional information: Specified argument was out of the range of valid values."


I checked the value of myObj and it was equal to 'Nothing' while e.Index = -1. Any reasons as to what is going on and how to fix it?

Jeff Armstrong
Sr. Systems Mgr
SBC Services, Inc.

jaucourt November 9th, 2004 04:55 AM

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.

Code:

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(104, 8)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Button2"
        '
        'ListBox1
        '
        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
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(184, 157)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    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)
        Else
            ' 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)
        Else
            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...
        myBrush.Dispose()
        myFont.Dispose()

    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


ja8261 November 10th, 2004 12:44 PM

Thanks for sticking with me on this one. I've got the code working. It makes much more sense when I saw that you created a class. I also didn't have ListBox1.Enabled = False set.

Jeff Armstrong
Sr. Systems Mgr
SBC Services, Inc.

jaucourt November 10th, 2004 06:40 PM

Quote:

quote:Originally posted by ja8261
 Thanks for sticking with me on this one. I've got the code working. It makes much more sense when I saw that you created a class. I also didn't have ListBox1.Enabled = False set.

Jeff Armstrong
Sr. Systems Mgr
SBC Services, Inc.
Careful! The ListBox1.Enabled = False is a red herring, a mistake on my part. It doesn't matter whether the listbox is enabled or not - probably best to have it enabled. Now you've got the code working, though, just set the listbox to enabled in the forms designer and everything should still work ok. Good luck with the app.


JCML April 9th, 2007 09:28 AM

This is a great solution, but is it possible to do anything similar in VBA for Access? I have a listbox where I want to change the text colour for different rows based on the data in each row. I can't seem to get the OnDrawItem event to work here. Is it not possible or is there something I am missing??

Thanks

John


LuxCoder April 9th, 2007 02:15 PM

Hey JauCourt,

Can u help me with this ->
 I have three forms:
1.MDI Parent (Master.vb)
2.MDI child (Customer.vb) &
3.MDI child (Orders.vb).

On MDI Parent i have a panel (just below the menubar) which has the following buttons:

1. New
2. Edit
3. Save
4. Delete
5. Undo
6. Redo
7. First
8. Previous
9. Next
10. Last
11. Search
12. Print
13. Close

Now my problem is how do i determine the active MDIChild and control its data with the buttons located at Master.vb panel ?


All times are GMT -4. The time now is 06:25 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.