更改列表框上包含在drawitem上的特定字符串的特定项目的颜色 [英] Change color of specific item on listbox that contains a specific string on drawitem

查看:77
本文介绍了更改列表框上包含在drawitem上的特定字符串的特定项目的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改包含特定字符串的项目的颜色

i want to change the color of item that contains a specific string

Private Sub ListBox2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox2.DrawItem
    e.DrawBackground()
    If DrawItemState.Selected.ToString.Contains("specific string") Then
        e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds)
    End If

    e.DrawFocusRectangle()

那是我的代码,但是不起作用

that is my code but not working

推荐答案

好的,首先需要将列表框的DrawMode属性设置为"OwnerDrawFixed",而不是Normal.否则,您将永远不会触发DrawItem事件.完成后,一切都非常简单.

Alright, first you need to set the property DrawMode of the list box to "OwnerDrawFixed" instead of Normal. Otherwise you will never get the DrawItem event to fire. When that is done it is all pretty straight forward.

Private Sub ListBox1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    e.DrawBackground()

    If ListBox1.Items(e.Index).ToString() = "herp" Then

        e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds)
    End If
    e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), e.Font, Brushes.Black, New System.Drawing.PointF(e.Bounds.X, e.Bounds.Y))
    e.DrawFocusRectangle()
End Sub

如果选择此选项,则必须用不同的颜色进行修饰.但这足以让您继续努力.您很亲近,请记住这一点. :)

You will have to touch this up with different colors if selected. But this should be enough for you to continue to work on. You were close, keep that in mind. :)

这篇关于更改列表框上包含在drawitem上的特定字符串的特定项目的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆