我想更改包含特定字符串的项目的颜色
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()
这是我的代码,但不起作用
好吧,首先你需要将列表框的属性DrawMode设置为“OwnerDrawFixed”而不是Normal。否则,您将永远不会触发 DrawItem 事件。完成后,一切都非常简单。
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
如果选择,您将必须用不同的颜色对其进行修饰。但这应该足以让您继续工作。你很接近,记住这一点。 :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)