开发者

listbox ownerdrawn items vb2008

开发者 https://www.devze.com 2023-02-16 17:58 出处:网络
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

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


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 ownerdrawn items vb2008


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号