具有不同ForeColors的ListBox项

时间:2018-08-15 06:16:02

标签: vb.net

如何为ForeColor中的每个项目放置不同的ListBoxenter image description here

上图是DevExpress中控件的示例。

1 个答案:

答案 0 :(得分:0)

我能够通过它。谢谢,jmcilhinney。 enter image description here

Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
    e.DrawBackground()
    If e.State = DrawItemState.Focus Then e.DrawFocusRectangle()
    If e.Index < 0 OrElse e.Index >= Items.Count Then Return
    Dim item As String = Items(e.Index)
    Dim lists As String() = item.Split(",")
    Dim brush As Brush() = {Brushes.Blue, Brushes.DarkOrange, Brushes.Black, Brushes.Red}
    Dim current As String = String.Empty
    e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
        current = String.Join(" ", lists)
        e.Graphics.DrawString(current, e.Font, Brushes.White, e.Bounds)
    Else
        For i As Integer = 0 To lists.Count - 1
            Dim size As SizeF = e.Graphics.MeasureString(current, e.Font)
            e.Graphics.DrawString(lists(i), e.Font, brush(i), size.Width, e.Bounds.Top, StringFormat.GenericDefault)
            current &= lists(i) & " "
        Next
    End If
End Sub