none
怎么修改listbox单行的字体颜色 RRS feed

  • 问题

  • 比如说我想把第三行的字改成红色。请问怎么弄?谢谢了!

    第一行

    第二行

    第三行 

    第四行

     

    2011年8月8日 4:35

答案

  • 您好

    請參考以下代碼

    Public Class Form1
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.DrawMode = DrawMode.OwnerDrawFixed
        Me.ListBox1.Items.AddRange(New String() {"第一行", "第二行", "第三行", "第四行"})
      End Sub
    
      Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        Dim lb As ListBox = CType(sender, ListBox)
        If e.Index = -1 Then
          Return
        End If
        If (e.Index = 2) Then
          e.Graphics.DrawString(Me.ListBox1.Items(e.Index), Me.ListBox1.Font, Brushes.Red, e.Bounds.X, e.Bounds.Y)
        Else
          e.Graphics.DrawString(Me.ListBox1.Items(e.Index), Me.ListBox1.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)
        End If
      End Sub
    End Class
    
    



    歡迎參觀我的Blog.NET菜鳥自救會
    2011年8月8日 6:01
  • 比如说我想把第三行的字改成红色。请问怎么弄?谢谢了!

    第一行

    第二行

    第三行 

    第四行

     


    你用WinForm做,那么请重写DrawItem事件:

    Private Sub Form1_Load(sender As Object, e As EventArgs)
    listBox1.DrawMode = DrawMode.OwnerDrawFixed
    '设置重绘模式
    End Sub

    Private Sub listBox1_DrawItem(sender As Object, e As DrawItemEventArgs)
    Dim bh As Brush = Nothing
    If e.Index = 2 Then
    '第三行重新绘色
    bh = Brushes.Red
    Else
    bh = Brushes.Black
    End If

    e.Graphics.DrawString(listBox1.Items(e.Index).ToString(), e.Font, bh, e.Bounds, StringFormat.GenericDefault)
    End Sub


    如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年8月8日 5:49
    版主
  • 您好

    請參考以下代碼

    Public Class Form1
      Dim bChangeColor = False
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.DrawMode = DrawMode.OwnerDrawFixed
        Me.ListBox1.Items.AddRange(New String() {"a", "b", "a", "c", "d", "a", "e"})
      End Sub
    
      Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        Dim myBrush As Brush = Brushes.Black
    
        If bChangeColor AndAlso (Me.ListBox1.Items(e.Index).ToString.Equals("a")) Then
          myBrush = Brushes.Red
        End If
        e.Graphics.DrawString(Me.ListBox1.Items(e.Index), Me.ListBox1.Font, myBrush, e.Bounds.X, e.Bounds.Y)
      End Sub
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        bChangeColor = Not bChangeColor
        Me.Refresh()
      End Sub
    End Class
    
    


    一開始

    按下 Button1


    歡迎參觀我的Blog.NET菜鳥自救會
    2011年8月8日 13:19

全部回复

  • 比如说我想把第三行的字改成红色。请问怎么弄?谢谢了!

    第一行

    第二行

    第三行 

    第四行

     


    你用WinForm做,那么请重写DrawItem事件:

    Private Sub Form1_Load(sender As Object, e As EventArgs)
    listBox1.DrawMode = DrawMode.OwnerDrawFixed
    '设置重绘模式
    End Sub

    Private Sub listBox1_DrawItem(sender As Object, e As DrawItemEventArgs)
    Dim bh As Brush = Nothing
    If e.Index = 2 Then
    '第三行重新绘色
    bh = Brushes.Red
    Else
    bh = Brushes.Black
    End If

    e.Graphics.DrawString(listBox1.Items(e.Index).ToString(), e.Font, bh, e.Bounds, StringFormat.GenericDefault)
    End Sub


    如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年8月8日 5:49
    版主
  • 您好

    請參考以下代碼

    Public Class Form1
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.DrawMode = DrawMode.OwnerDrawFixed
        Me.ListBox1.Items.AddRange(New String() {"第一行", "第二行", "第三行", "第四行"})
      End Sub
    
      Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        Dim lb As ListBox = CType(sender, ListBox)
        If e.Index = -1 Then
          Return
        End If
        If (e.Index = 2) Then
          e.Graphics.DrawString(Me.ListBox1.Items(e.Index), Me.ListBox1.Font, Brushes.Red, e.Bounds.X, e.Bounds.Y)
        Else
          e.Graphics.DrawString(Me.ListBox1.Items(e.Index), Me.ListBox1.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)
        End If
      End Sub
    End Class
    
    



    歡迎參觀我的Blog.NET菜鳥自救會
    2011年8月8日 6:01
  • 谢谢楼上几位。但是我要标记的地方是不一定的,数量也不一定的。

    再举个例子,我要标记"a"为红色,见表格:

    a

    b

    a

    c

    d

    a

    e

    单击Button1后

     

    a

    b

    a

    c

    d

    a

    e

     

    2011年8月8日 6:29
  • 您好

    請參考以下代碼

    Public Class Form1
      Dim bChangeColor = False
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ListBox1.DrawMode = DrawMode.OwnerDrawFixed
        Me.ListBox1.Items.AddRange(New String() {"a", "b", "a", "c", "d", "a", "e"})
      End Sub
    
      Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        Dim myBrush As Brush = Brushes.Black
    
        If bChangeColor AndAlso (Me.ListBox1.Items(e.Index).ToString.Equals("a")) Then
          myBrush = Brushes.Red
        End If
        e.Graphics.DrawString(Me.ListBox1.Items(e.Index), Me.ListBox1.Font, myBrush, e.Bounds.X, e.Bounds.Y)
      End Sub
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        bChangeColor = Not bChangeColor
        Me.Refresh()
      End Sub
    End Class
    
    


    一開始

    按下 Button1


    歡迎參觀我的Blog.NET菜鳥自救會
    2011年8月8日 13:19
  • 谢谢楼上几位。但是我要标记的地方是不一定的,数量也不一定的。

    再举个例子,我要标记"a"为红色,见表格:

    a

    b

    a

    c

    d

    a

    e

    单击Button1后

     

    a

    b

    a

    c

    d

    a

    e

     


    无论你如何变化,请参照我第一个例子啊,你判断索引(究竟在哪个位置出现,然后把画笔改成红色就可以了)。

    另外,你的话很别扭——但是我要标记的地方是不一定的,数量也不一定的——既然“不确定”,就是没有规律了?这怎么编码呢?希望你先整理好思路。


    如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年8月9日 0:42
    版主