-1

我需要根据要添加的项目的文本或其包含的文本在列表框中绘制每个项目。然后我需要在列表框的开头放置一个图标,根据我指定的单词使用另外两种颜色和图标,例如,

  • 如果项目包含错误文本,请在开头放置一个错误(16x16px)图标,并以浅红色绘制背景,以深红色粗体绘制文本。

  • 如果它包含准备好的或开始的文本,则使用浅橙色背景和深蓝色粗体文本。

  • 如果它包含 ok 或 success 文本,则使用浅绿色背景和深绿色粗体文本。

我怎样才能做到这一点?

编辑

这是我已经拥有的,但这段代码似乎会不断刷新。我需要选择颜色的地方是 e.index 的值。我可以将 e.index 更改为像 e.stringvalue 这样的 somthinf 吗?

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
4

1 回答 1

1

回答您的两个具体问题:

  1. 您显示的代码会不断刷新,因为您在Refresh最后添加了一个调用:

    lsbLog.Refresh()
    

    把它拿出来,你将解决无休止的刷新问题。

  2. 是的,您当然可以测试项目的标题而不是其索引,但没有e.stringvalue. 您必须以不同的方式来处理它,一种您已经发现并在调用中使用的方式DrawString

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

    你可能想做一些比我更复杂的事情,这取决于项目通常包含的内容。例如,您可能想要检查字符串是否包含关键字,而不是测试是否相等。为了获得更大的灵活性,您可能需要Select CaseIf-Else语句替换。


因此,稍后进行一些小的修改,我最终得到以下代码:

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

结果如下:

   具有所有者绘制项目的列表框


当然,要完全满足您的要求,您还需要填写每个项目的背景。最好的方法是使用以下内容,但相应地更改画笔颜色:

e.Graphics.FillRectangle(Brushes.Coral, e.Bounds)
于 2011-03-15T09:32:20.427 回答