【问题标题】:listbox ownerdrawn items vb2008列表框所有者绘制项目 vb 2008
【发布时间】:2011-03-15 08:48:36
【问题描述】:

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

  • 如果项目包含错误文本, 在 开始和绘制背景 浅红色和粗体字 深红色。

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

  • 如果它包含 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

【问题讨论】:

  • 您发布了一系列要求,而不是问题。这里没有人会为您编写代码。你已经尝试了什么?你现在有什么代码?你需要自己先下功夫解决这个问题。
  • 你的评论语法很奇怪。 VB.NET 不使用 C 风格的 cmets (//)。您只需要'
  • @Cody Gray 谢谢你,但我就是喜欢这样,这是我在 php、c++、c# 和 JavaScript 中的评论风格,所以我尝试在 vb 中调整类似的东西

标签: .net vb.net winforms listbox ownerdrawn


【解决方案1】:

回答您的两个具体问题:

  1. 您显示的代码会不断刷新,因为您在末尾添加了对Refresh 的调用:

    lsbLog.Refresh()
    

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

  2. 是的,你当然可以测试项目的标题而不是它的索引,但是没有像e.stringvalue 这样的属性。您必须以不同的方式来处理它,这是您在致电DrawString 时已经发现并使用的方式:

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

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

【讨论】:

  • 非常感谢,但我把这个e.Graphics.FillRectangle(Brushes.Coral, e.Bounds)
  • @Smith:您在最顶部调用e.DrawBackground,它为项目绘制默认背景。每当您不想用自定义颜色填充背景时,您都想使用它。否则,您只需将此调用替换为 e.Graphics.FillRectangle 并指定您选择的颜色画笔。但是,是的,您需要在绘制文本之前填充背景,以免文本被背景覆盖!
  • 我如何将颜色或所选项目文本(如果超过一个)更改为白色,如果没有取消选择,则返回其原始颜色。
  • @Smith:奖励积分的唯一方法是投票并接受答案。为了更改颜色,您必须在 DrawItem 事件处理程序中添加更多代码(所有者绘图意味着您确实必须控制 一切,这比让系统做更多的工作做)。检查是否选择了该项目(使用e.Item 属性作为索引),如果是,则以一种颜色绘制文本。如果不是,请用不同的颜色绘制文本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多