【发布时间】: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