老板画的列表框

介绍 这是一个列表框

  

,它支持用自定义前颜色绘制列表框项。可以很容易地对其进行修改以支持更多特性,但我保持了它的简单性,并允许其他人进行探索。 列表框绘制模式已更改为: 隐藏,复制Code

ListBox.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed

此属性更改告诉列表框引发DrawItem事件。DrawItem将负责绘制所有清单项目。在本例中,列表框将尝试将每个列表项强制转换为ICustomListBoxItemSupport接口;如果这工作,它将随后拉TextColor并使用接口显示值。这允许每个列表项在绘制文本时提供自己的颜色。如果cast不起作用,列表框应该仍然在现有列表框功能附近工作。 使用ICustomListBoxItemSupport接口的好处是,您可以在继承自其他对象的现有对象中实现接口,这使得添加到现有业务对象更加容易。 背景 我需要一种方法来指出一些列表项是不同的(在我的例子中是错误的)。 使用的代码 让您的列表项实现ICustomListBoxItemSupport接口,自定义列表框将完成其余工作。 隐藏,收缩,复制Code

Imports System.Drawing

''' <summary>
''' Example custom list box item.
''' </summary>
''' <remarks></remarks>
Public Class Person

    Implements ICustomListBoxItemSupport

    Private m_Name As String = String.Empty

    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property


#Region "ICustomListBoxItemSupport implements"
 
    Public ReadOnly Property TextColor() As System.Drawing.Color _
           Implements ICustomListBoxItemSupport.TextColor
        Get
            If m_Name.Length > 25 Then
                Return Color.DarkRed
            Else
                Return SystemColors.WindowText
            End If
        End Get
    End Property

    Public Property DisplayValue() As String _
           Implements ICustomListBoxItemSupport.DisplayValue
        Get
            Return Me.Name
        End Get
        Set(ByVal value As String)

        End Set
    End Property

#End Region

End Class

本文转载于:http://www.diyabc.com/frontweb/news342.html

原文地址:https://www.cnblogs.com/Dincat/p/13437896.html