更改包含drawitem上特定字符串的列表框上特定项目的颜色

更改包含drawitem上特定字符串的列表框上特定项目的颜色

本文介绍了更改包含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上特定字符串的列表框上特定项目的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:38