列表框项目forecolor更改 [英] listbox item forecolor change

查看:72
本文介绍了列表框项目forecolor更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道为什么Listbox1.Refresh()命令每次都不能触发ListBox1_DrawItem子?



在Microsoft Visual Basic 2010中,列表框中有一个forcolor和backcolor属性。这些属性会更改列表框中所有项目的forcolour和backcolor。默认情况下,列表框中的单个项目的forecolor和backcolor没有属性,我知道列表视图中有,但我仍然希望使用列表框。我试图能够更改列表框中各个项目的forecolor和backcolor属性。要执行此操作,列表框的绘图项子必须与列表框的drawmode属性设置为OwnerDrawFixed一起使用。然后使用画笔颜色和e.graphics可以改变前景色或背景色。我已经看到并遵循了如何为当前所选项目执行此操作的示例。比如来自ehow网站的那个。然而,令我厌倦的是改变litsbox项目的颜色,因为它是根据变量添加的。



这是我的代码:

 私有  Sub  listbox_add()

.ListBox1.Items.Add(listbox_text(list_num))' 将行添加到列表框
add_item_colour = True
ListBox1.Refresh()

结束 Sub



< span class =code-keyword>私有 Sub ListBox1_DrawItem( ByVal sender 作为 对象 ByVal e As System.Win dows.Forms.DrawItemEventArgs)句柄 ListBox1.DrawItem
Dim myBrush As Brush = Brushes.Black

e.DrawBackground()
如果 add_item_colour = True 然后
如果 blue_message = < span class =code-keyword> True 然后
myBrush = Brushes.Blue
Else
myBrush = Brushes.Black
结束 如果
e.Graphics.DrawString(ListBox1.Items.Item(list_num),ListBox1.Font,myBrush,_
RectangleF(e.Bounds .X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height))
add_item_colour = 错误
结束 如果
e。 DrawFocusRectangle()
结束 Sub





listbox_text是一个字符串数组,用于存储要添加的字符串,list_num是一个整数,当新项目添加到列表框时会递增,而blue_message是一个布尔值,当我想要它时它是真的一个蓝色的消息,当我不这样做时就是假的。



我似乎遇到的问题是Listbox1.Refresh()命令似乎没有触发ListBox1_DrawItem sub每次被调用。我通过使用制动点找到了这个。有谁知道为什么会出现这种情况以及如何解决这个问题?



谢谢,对此的任何帮助都会非常感激。

解决方案

成功示例 - 2013年9月5日修订10:57 EDT包含RemoveAt

以下代码有效。

我在Windows 8 PC上使用Visual Studio 2012进行了测试。



通过使用Visual Studio Interactive Debugger,我确定在 ListBox 中的所有可见行都会在刷新时刷新添加了新行,如果新行不可见,则不会绘制新行。因此,我必须在添加新行之前为集合中的每一行保存画笔颜色,并在每次绘制关联的ListBox行时使用该画笔颜色。我使用Collection Class来保存ListBox每一行的画笔颜色。



刷新方法不是需要。



  Dim  colColors 正如 Collection = 集合' 空间到为ListBox中的每一行保存画笔颜色 

私有 Sub Form1_Load(发件人作为 对象,e As EventArgs)句柄 .Load
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
结束 Sub

私有 Sub listbox_add_Blue()
SetColorForNewRow(Brushes.Blue)
ListBox1.Items.Add( Blue 将行添加到列表框
结束 Sub

私有 Sub listbox_add_Black()
SetColorForNewRow(Brushes.Black)
ListBox1.Items.Add( 黑色' 添加行到列表框
结束 Sub

Sub listbox_remove_0()
如果 ListBox1.Items.Count< 1 然后 退出 Sub
' 必须先从colColors集合中删除画笔颜色!
colColors.Remove( 1 ' 集合索引从1开始
ListBox1.Items.RemoveAt( 0
结束 Sub

私有 Sub ListBox1_DrawItem( ByVal sender As 对象,_
ByVal e As System.Windows.Forms.DrawItemEventArgs )句柄 ListBox1.DrawItem
e.DrawBackground()
如果 e.Index> = 0 AndAlso e.Index< ListBox1.Items.Count 然后
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(),_
e。 Font,colColors.Item( 1 + e.Index),_
e.Bounds,StringFormat.GenericDefault)
结束 如果
e.DrawFocusRectangle()
结束 Sub

Sub SetColorForNewRow( ByVal MyBrush As Brush)
colColors.Add(MyBrush)
End Sub

私有 Sub btnBlack_Click(发件人作为 对象,e As EventArgs)句柄 btnBlack.Click
listbox_add_Black()
结束 Sub

私有 Sub btnBlue_Click(发件人作为 对象,e As EventArgs)句柄 btnBlue.Click
listbox_add_Blue()
结束 Sub

私有 Sub btnRemove_Click(sender As Object ,e As EventArgs)句柄 btnRemove.Click
listbox_remove_0()
结束 Sub









_____________________________________________________________________________________________

首次尝试使用数组存储画笔颜色(部分成功)

以下代码适用于 ListBox 中的少量项目。我在Windows 8 PC上使用Visual Studio 2012进行了测试。当垂直滚动条处于活动状态时,我仍在研究如何处理 DrawItem 事件。



使用Visual Studio Interactive Debugger,我确定在添加新行时刷新 ListBox 中的所有行。因此,我必须为数组中的每一行保存画笔颜色,并在每次重绘关联的ListBox行时使用该画笔颜色。不需要刷新方法。



使用 ListView 控件可以轻松实现。



  Dim  blue_message  As  布尔 = 错误 ' 使用黑色或蓝色笔刷 
Dim AddMode As 布尔 = 错误 ' 告诉DrawItem它处于Add to ListBox模式
Dim intNewIndex As 整数 ' 要添加的新项目的索引到ListBox
Dim arrColor s( 0 100 作为' 为ListBox中的每一行保存画笔颜色的空间

私有 Sub Form1_Load(sender As Object ,e As EventArgs)句柄 .Load
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
结束 Sub

私有 Sub listbox_add_Blue()
blue_message = True
AddMode = True
intNewIndex = ListBox1.Ite ms.Count
ListBox1.Items.Add( Blue' 将行添加到列表框
结束 Sub

私有 Sub listbox_add_Black()
blue_message = False
AddMode = True
intNewIndex = ListBox1.Items.Count
ListBox1.Items.Add( Black' 将行添加到列表框
结束 Sub

私有 Sub ListBox1_DrawItem( ByVal sender 作为 对象 ByVal e < span class =code-keyword> As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim myBrush As Brush = Brushes.Black
e.DrawBackground()
如果 AddMode AndAlso e.Index = intNewIndex 那么
如果 blue_message 那么
myBrush = Brushes.Blue
结束 如果
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(),e.​​Font,myBrush ,_
e.Bounds,StringFormat.GenericDefault)
如果 e.Index > arrColors.GetUpperBound( 0 然后 ' 数组是否填充
ReDim 保留arrColors( 0 arrColors.Count + 100 ' 腾出更多空间
结束 如果
arrColors(e.Index)= myBrush ' 保存我们使用的画笔
AddMode = 错误 ' 已完成绘制新项目(它是ListBox的最后一行
Else
e.Graphics.DrawString(ListBox1.Items (e.Index).ToString(),_
e.Font,arrColors(e.Index),_
e.Bounds,StringFormat.GenericDefault)
结束 如果
e.DrawFocusRectangle()
结束 Sub

私有 Sub btnBlack_Click(发件人作为 对象,e 作为 EventArgs)句柄 btnBlack.Click
listbox_add_Black()
结束 Sub

私有 Sub btnBlue_Click(发件人作为 对象,e 作为 EventArgs)句柄 btnBlue.Click
listbox_add_Blue()
结束 Sub







_____________________________________________________________________________________________

解决方案的原始版本

我查看了ListBox.DrawItem事件的Microsoft帮助页面,并对进行了一些更改e.Graphics.DrawString 声明。另外,请注意我移动了 add_item_colour = True 的位置。



我没有测试。



 私人  Sub  listbox_add()
add_item_colour = True
Me .ListBox1。 Items.Add(listbox_text(list_num))' 将行添加到列表框
ListBox1 .Refresh()
结束 Sub

私有 Sub ListBox1_DrawItem( ByVal sender 作为 对象 ByVal e 作为 System.Windows.Forms.DrawItemEventArgs)句柄 ListBox1.DrawItem
Dim myBrush As Brush = Brushes.Black

e.DrawBackground()
如果 add_item_colour = True 然后
如果 blue_message = True 然后
myBrush = Brushes.Blue
其他
myBrush = Brushes.Black
结束 如果
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(),e.​​Font,myBrush,_
e.Bounds,StringFormat.GenericDefault)
add_item_colour = 错误
结束 如果
e.DrawFocusRectangle()
结束 Sub


Does anyone know why the Listbox1.Refresh() command may not trigger the ListBox1_DrawItem sub every time?

In Microsoft Visual Basic 2010, a listbox has a forcolor and backcolor property. These properties change the forcolour and backcolor for all the items in the listbox. By default there is no property for the forecolor and backcolor of an individual item on a listbox, I am aware there is on a list view but I would still wish to use a listbox. I am trying to have the ability to change the forecolor and backcolor properties of individual items in the listbox. To do this the listbox's draw item sub must be used with the listbox's drawmode property set to OwnerDrawFixed. Then using a brush colour along with the e.graphics the forecolor or backcolor can be changed. I have seen and followed examples of how to do this for the currently selected item. Such as the one from ehow's website. What I am tiring to do however is change the colour of the litsbox item as it is added depending on a variable.

Here is my code:

Private Sub listbox_add()

        Me.ListBox1.Items.Add(listbox_text(list_num)) ' adds the line to the list box
        add_item_colour = True
        ListBox1.Refresh()

End Sub



    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    Dim myBrush As Brush = Brushes.Black

    e.DrawBackground()
    If add_item_colour = True Then
        If blue_message = True Then
            myBrush = Brushes.Blue
        Else
            myBrush = Brushes.Black
        End If
        e.Graphics.DrawString(ListBox1.Items.Item(list_num), ListBox1.Font, myBrush, _
        New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
        add_item_colour = False
    End If
    e.DrawFocusRectangle()
End Sub



The listbox_text is a string array that stores the string being added, the list_num is a integer that increments when new items are added to the listbox and the blue_message is a Boolean that it true when I want a blue message and false when I don't.

The problem I seem to be having is that Listbox1.Refresh() command does not seem to be triggering the ListBox1_DrawItem sub every time it is called. I found this by using brake points. Does anyone know why this might be the case and how I could fix it?

Thanks, any help on this would be much appreciated.

解决方案

Successful Example - Revised 5 Sep 2013 10:57 EDT to include RemoveAt
The following code works.
I tested using Visual Studio 2012 on a Windows 8 PC.

By using the Visual Studio Interactive Debugger, I determined that all visible rows in the ListBox are refreshed when a new row is added and that if a new row is not visible, it is not drawn. Therefore, I had to save the brush color for each row in a collection before adding the new row and use that brush color each time the associated ListBox row is drawn. I use Collection Class to save the brush colors for each row of the ListBox.

The Refresh method was not needed.

Dim colColors As Collection = New Collection ' Space to save brush colors for each row in the ListBox

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ListBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub

Private Sub listbox_add_Blue()
    SetColorForNewRow(Brushes.Blue)
    ListBox1.Items.Add("Blue") ' adds the line to the list box
End Sub

Private Sub listbox_add_Black()
    SetColorForNewRow(Brushes.Black)
    ListBox1.Items.Add("Black") ' adds the line to the list box
End Sub

Sub listbox_remove_0()
    If ListBox1.Items.Count < 1 Then Exit Sub
    ' Must remove brush color from colColors collection first!
    colColors.Remove(1) ' Collection index starts at 1
    ListBox1.Items.RemoveAt(0)
End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    e.DrawBackground()
    If e.Index >= 0 AndAlso e.Index < ListBox1.Items.Count Then
        e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _
            e.Font, colColors.Item(1 + e.Index), _
            e.Bounds, StringFormat.GenericDefault)
    End If
    e.DrawFocusRectangle()
End Sub

Sub SetColorForNewRow(ByVal MyBrush As Brush)
    colColors.Add(MyBrush)
End Sub

Private Sub btnBlack_Click(sender As Object, e As EventArgs) Handles btnBlack.Click
    listbox_add_Black()
End Sub

Private Sub btnBlue_Click(sender As Object, e As EventArgs) Handles btnBlue.Click
    listbox_add_Blue()
End Sub

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    listbox_remove_0()
End Sub





_____________________________________________________________________________________________
First attempt using an array to store brush colors (Partial Success)
The following code works for small number of items in the ListBox. I tested using Visual Studio 2012 on a Windows 8 PC. I am still studying how to handle the DrawItem event when the vertical scroll bar is active.

By using the Visual Studio Interactive Debugger, I determined that all rows in the ListBox are refreshed when a new row is added. Therefore, I had to save the brush color for each row in an array and use that brush color each time the associated ListBox row is redrawn. The Refresh method was not needed.

This would have been a lot easier with a ListView control.

Dim blue_message As Boolean = False ' Use Black or Blue Brush
Dim AddMode As Boolean = False  ' Tells DrawItem that it is in Add to ListBox mode
Dim intNewIndex As Integer ' The index of the new item to be added to the ListBox
Dim arrColors(0 To 100) As Brush ' Space to save brush colors for each row in the ListBox

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ListBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub

Private Sub listbox_add_Blue()
    blue_message = True
    AddMode = True
    intNewIndex = ListBox1.Items.Count
    ListBox1.Items.Add("Blue") ' adds the line to the list box
End Sub

Private Sub listbox_add_Black()
    blue_message = False
    AddMode = True
    intNewIndex = ListBox1.Items.Count
    ListBox1.Items.Add("Black") ' adds the line to the list box
End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    Dim myBrush As Brush = Brushes.Black
    e.DrawBackground()
    If AddMode AndAlso e.Index = intNewIndex Then
        If blue_message Then
            myBrush = Brushes.Blue
        End If
        e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), e.Font, myBrush, _
            e.Bounds, StringFormat.GenericDefault)
        If e.Index > arrColors.GetUpperBound(0) Then ' Is the array filled
            ReDim Preserve arrColors(0 To arrColors.Count + 100) ' Make some more room
        End If
        arrColors(e.Index) = myBrush ' Save which brush we used
        AddMode = False ' Finished drawing the new item (It is the last row of the ListBox
    Else
        e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _
            e.Font, arrColors(e.Index), _
            e.Bounds, StringFormat.GenericDefault)
    End If
    e.DrawFocusRectangle()
End Sub

Private Sub btnBlack_Click(sender As Object, e As EventArgs) Handles btnBlack.Click
    listbox_add_Black()
End Sub

Private Sub btnBlue_Click(sender As Object, e As EventArgs) Handles btnBlue.Click
    listbox_add_Blue()
End Sub




_____________________________________________________________________________________________
Original Version of the Solution
I looked at the Microsoft Help page for ListBox.DrawItem event and made a few changes to e.Graphics.DrawString statement. Also, notice that I moved the location of add_item_colour = True.

I did not test.

Private Sub listbox_add()
        add_item_colour = True
        Me.ListBox1.Items.Add(listbox_text(list_num)) ' adds the line to the list box
        ListBox1.Refresh()
End Sub

    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    Dim myBrush As Brush = Brushes.Black

    e.DrawBackground()
    If add_item_colour = True Then
        If blue_message = True Then
            myBrush = Brushes.Blue
        Else
            myBrush = Brushes.Black
        End If
        e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), e.Font, myBrush, _
            e.Bounds, StringFormat.GenericDefault)
        add_item_colour = False
    End If
    e.DrawFocusRectangle()
End Sub


这篇关于列表框项目forecolor更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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