各位大侠好!
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        Dim datas As String() = sData
        Dim rc As RectangleF = New RectangleF(e.Bounds.X + 3, e.Bounds.Y + 5, e.Bounds.Width - 5, e.Bounds.Height - 3)
        Dim rc1 As Rectangle = New Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 5, e.Bounds.Height - 3)
        Dim myBrush As Brush = Brushes.Black
        e.DrawBackground()
        '在选定项目上绘制选定背景色
        'e.DrawFocusRectangle ( ) 
        Dim sf As StringFormat = New StringFormat()
        sf.Alignment = StringAlignment.Near
        '设定 ListBox 中项目名称的在矩形区域的排列对齐方式
        'e.Graphics.DrawRectangle(New Pen(New SolidBrush(System.Drawing.Color.White), 1), rc1)
        '以线条粗度为1,黑色画笔对绘制ListBox中的每一个项目的边框
        e.Graphics.DrawString(sData(e.Index), New Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold), New SolidBrush(sColorData(e.Index)), rc, sf)        Select Case ListBox1.Items(e.Index.ToString)
            Case "关于"
                myBrush = Brushes.White
            Case "界面背景"
                myBrush = Brushes.White
            Case Else
                myBrush = Brushes.Black
        End Select        e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, New Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold), myBrush, rc, sf)
        e.DrawFocusRectangle()
        ' 以指定的字体大小、类型、颜色绘制 ListBox 项目名称
end sub
结果成这样子:
     主要的就是颜色改变不对。

解决方案 »

  1.   


    Private sColorData() As Object = {System.Drawing.Color.Black, System.Drawing.Color.Black}
        Private sColorData2() As Object = {System.Drawing.Color.White, System.Drawing.Color.White}
      

  2.   

    你给 myBrush赋值的时候要判断e.Index是不是SelectedIndex,如果是,使用白色没有问题;不如果不是,使用其他颜色,要不背景也是颜色,就看不到了.
      

  3.   

    不知道怎么弄了,好像是只要改动了selecteditem,就把所有颜色改了
      

  4.   

    好的,找到了例子公布答案:
     bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
                if (!selected)
                {
                    lgb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.Horizontal);
                }
                else
                {
                    lgb = new LinearGradientBrush(r, Color.Cyan, Color.White, LinearGradientMode.Horizontal);
                }
    加上这一句就好了!