-1

I need to draw each item in a listbox based on the text the items that is about to be added or the text it contains. Then I need to place an icon at the beginning of the listbox, with two other colors and icons depending on words I specify, e.g,

  • If the item contains error text, place an error(16x16px) icon at the beginning and the draw the background in light red and the text in bold dark red.

  • If it contains an ready or starting text, then use light orange background and dark bold blue font text.

  • If it contains an ok or success text, then use light green background and dark bold green font text.

How can I do this?

EDIT

Here is what i already have, but this code seem to refresh endless. where i need to choose color is the the value of e.index. Can i change the e.index to somthinf like e.stringvalue?

Private Sub lsbLog_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lsbLog.DrawItem
        '//Draw the background of the ListBox control for each item.
        '// Create a new Brush and initialize to a Black colored brush
        '// by default.
        e.DrawBackground()
        Dim mybrush = Brushes.Black

        '// Determine the color of the brush to draw each item based on 
        '//the index of the item to draw.
        Select Case e.Index
            Case 0
                mybrush = Brushes.Red
            Case 1
                mybrush = Brushes.Blue
            Case 2
                mybrush = Brushes.Green
        End Select

        '//
        '// Draw the current item text based on the current 
        '// Font and the custom brush settings.
        '//
        e.Graphics.DrawString(lsbLog.Items(e.Index).ToString(), _
                              e.Font, mybrush, e.Bounds, StringFormat.GenericDefault)
        '//
        '// If the ListBox has focus, draw a focus rectangle 
        '// around the selected item.
        '//
        e.DrawFocusRectangle()
        lsbLog.Refresh()
    End Sub
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Smith
  • 5,765
  • 17
  • 102
  • 161
  • 1
    You've posted a series of requirements, not a question. No one here is going to write the code for you. What have you already tried? What code do you have now? You need to put some effort into solving this problem yourself first. – Cody Gray - on strike Mar 15 '11 at 08:53
  • 1
    Your commenting syntax is very strange. VB.NET doesn't use C-style comments (`//`). All you need is the `'`. – Cody Gray - on strike Mar 15 '11 at 09:32
  • @Cody Gray Thanks for that, but i just like it that way, its my style of commenting in php,c++,c# and JavaScript, so i try to adapt something similar in vb – Smith Mar 15 '11 at 10:01

1 Answers1

1

In answer to your two specific questions:

  1. The code you've shown refreshes endlessly because you've added a call to Refresh at the end:

    lsbLog.Refresh()
    

    Take that out and you'll solve the endless refresh problem.

  2. Yes, you can certainly test the item's caption instead of its index, but there is no such property as e.stringvalue. You'll have to get at it a different way, a way that you've already discovered and used in your call to DrawString:

    lsbLog.Items(e.Index).ToString()
    

    You might want to do something a bit more complex than I have, depending on what the items will generally contain. For example, you may want to check if the string contains the keywords, rather than testing for equality. For more flexibility, you may need to replace Select Case with an If-Else statement.


So, a few minor modifications later, I end up with the following code:

Private Sub lsbLog_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles lsbLog.DrawItem
    '//Draw the background of the ListBox control for each item.
    '// Create a new Brush and initialize to a Black colored brush
    '// by default.
    e.DrawBackground()
    Dim mybrush As Brush = Brushes.Black

    '// Determine the color of the brush to draw each item based on 
    '//the index of the item to draw.
    Select Case lsbLog.Items(e.Index).ToString
        Case "Error"
            mybrush = Brushes.Red
        Case "Ready"
            mybrush = Brushes.Blue
        Case "Success"
            mybrush = Brushes.Green
    End Select

    '//
    '// Draw the current item text based on the current 
    '// Font and the custom brush settings.
    '//
    e.Graphics.DrawString(lsbLog.Items(e.Index).ToString(), _
                          e.Font, mybrush, e.Bounds, StringFormat.GenericDefault)
    '//
    '// If the ListBox has focus, draw a focus rectangle 
    '// around the selected item.
    '//
    e.DrawFocusRectangle()
End Sub

And here's what the result looks like:

   ListBox with owner drawn items


Of course, to fully meet your requirements, you'll also need to fill in the background of each item. The best way of doing that is using something like the following, but changing the brush color accordingly:

e.Graphics.FillRectangle(Brushes.Coral, e.Bounds)
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Many thanks, but where do i put this `e.Graphics.FillRectangle(Brushes.Coral, e.Bounds)` – Smith Mar 15 '11 at 10:12
  • 1
    @Smith: You call `e.DrawBackground` at the very top, which draws the *default* background for the item. You want to use this whenever you don't want to fill the background with a custom color. Otherwise, you would just replace this call with `e.Graphics.FillRectangle` and specify the color brush of your choice. But yeah, you need to fill the background *before* you draw the text, lest the text be covered up by the background! – Cody Gray - on strike Mar 15 '11 at 10:18
  • how do i change the color or the selected items text (in case if more than one) to white and back to their original color if not unselected. – Smith Mar 15 '11 at 10:42
  • 1
    @Smith: The only way to award points is by upvoting and accepting the answer. In order to change the color, you'll have to add more code in your `DrawItem` event handler (owner drawing means you really do have to control *everything*, it's a lot more work than letting the system do it). Check to see if the item is selected (using the `e.Item` property as the index), and if it is, draw the text in one color. If it isn't, draw the text in a different color. – Cody Gray - on strike Mar 15 '11 at 11:06