如何在标准的ListBox中增加自定义的Item
Public
Class ListStyle
Class ListStyle
Inherits System.Windows.Forms.ListBox
LineStyle Structure >>#Region "LineStyle Structure >>"
'line type:
' none(0)----->Custom
' (1)---->Solid
' (2)---->Dash
' (3)---->Dot
' (4)---->DashDot
' (5)---->DashDotDot
Public Structure StyleStructure Style
Public LineType As System.Drawing.Drawing2D.DashStyle
Public LineWidth As Single
'Public LineColor As System.Drawing.Color
End Structure
#End Region
Property>>#Region "Property>>"
'current line style(0--15)
Private _lineStyle As Style
Public Property LineStyle()Property LineStyle() As Style
Get
Return _lineStyle
End Get
Set(ByVal Value As Style)
_lineStyle = Value
End Set
End Property
'current line color
Private _lineColor As Color
Public Property LineColor()Property LineColor() As Color
Get
If _lineColor.Equals(Color.Empty) Then
_lineColor = Color.Black
End If
Return _lineColor
End Get
Set(ByVal Value As Color)
_lineColor = Value
End Set
End Property
Private _alternate As Integer = 5
#End Region
Subs/Functions>>#Region "Subs/Functions>>"
'draw the specified line in listbox
Protected Overrides Sub OnDrawItem()Sub OnDrawItem(ByVal e As DrawItemEventArgs)
MyBase.OnDrawItem(e)
Me.DrawMode = DrawMode.OwnerDrawFixed
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
Dim selectBrush As New SolidBrush(Me.BackColor)
e.Graphics.FillRectangle(selectBrush, e.Bounds)
Dim selectPen As New Pen(Color.Gray)
selectPen.DashStyle = Drawing2D.DashStyle.Dash
e.Graphics.DrawRectangle(selectPen, e.Bounds)
Else
Dim selectBrush As New SolidBrush(Me.BackColor)
e.Graphics.FillRectangle(selectBrush, e.Bounds)
Dim selectPen As New Pen(Me.BackColor)
e.Graphics.DrawRectangle(selectPen, e.Bounds)
End If
' e.DrawBackground()
e.DrawFocusRectangle()
Dim myPen As New Pen(Me.LineColor)
' Dim myPen As New Pen(Color.Red)
Select Case e.Index
Case 0
'none
Case 1
'solid
myPen.DashStyle = Drawing2D.DashStyle.Solid
Case 2
'dash
myPen.DashStyle = Drawing2D.DashStyle.Dash
Case 3
'dot
myPen.DashStyle = Drawing2D.DashStyle.Dot
Case 4
'dashdot
myPen.DashStyle = Drawing2D.DashStyle.DashDot
Case 5
'dashdotdot
myPen.DashStyle = Drawing2D.DashStyle.DashDotDot
Case 6
'solid
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.Solid
Case 7
'dash
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.Dash
Case 8
'dot
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.Dot
Case 9
'dashdot
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.DashDot
Case 10
'dashdotdot
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.DashDotDot
Case 11
'solid
myPen.Width = 3
myPen.DashStyle = Drawing2D.DashStyle.Solid
'Case 12
' 'dash
' myPen.Width = 3
' myPen.DashStyle = Drawing2D.DashStyle.Dash
'Case 13
' 'dot
' myPen.Width = 3
' myPen.DashStyle = Drawing2D.DashStyle.Dot
'Case 14
' 'dashdot
' myPen.Width = 3
' myPen.DashStyle = Drawing2D.DashStyle.DashDot
'Case 15
' 'dashdotdot
' myPen.Width = 3
' myPen.DashStyle = Drawing2D.DashStyle.DashDotDot
End Select
'e.Graphics.DrawRectangle(myPen, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
If e.Index = 0 Then
Dim myBrush As New SolidBrush(Color.Black)
e.Graphics.DrawString(Me.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y + _alternate, e.Bounds.Width, e.Bounds.Height))
Else
e.Graphics.DrawLine(myPen, e.Bounds.X + _alternate, e.Bounds.Y + CInt(e.Bounds.Height / 2), e.Bounds.X + e.Bounds.Width - _alternate, e.Bounds.Y + CInt(e.Bounds.Height / 2))
End If
End Sub
'set current selectedindex
Private Sub ListStyle_SelectedIndexChanged()Sub ListStyle_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SelectedIndexChanged
Dim currentStyle As Style
Select Case Me.SelectedIndex
Case 0
currentStyle.LineType = Drawing2D.DashStyle.Custom 'represent for none preset
currentStyle.LineWidth = 1
Case 1
currentStyle.LineType = Drawing2D.DashStyle.Solid
currentStyle.LineWidth = 1
Case 2
currentStyle.LineType = Drawing2D.DashStyle.Dash
currentStyle.LineWidth = 1
Case 3
currentStyle.LineType = Drawing2D.DashStyle.Dot
currentStyle.LineWidth = 1
Case 4
currentStyle.LineType = Drawing2D.DashStyle.DashDot
currentStyle.LineWidth = 1
Case 5
currentStyle.LineType = Drawing2D.DashStyle.DashDotDot
currentStyle.LineWidth = 1
Case 6
currentStyle.LineType = Drawing2D.DashStyle.Solid
currentStyle.LineWidth = 2
Case 7
currentStyle.LineType = Drawing2D.DashStyle.Dash
currentStyle.LineWidth = 2
Case 8
currentStyle.LineType = Drawing2D.DashStyle.Dot
currentStyle.LineWidth = 2
Case 9
currentStyle.LineType = Drawing2D.DashStyle.DashDot
currentStyle.LineWidth = 2
Case 10
currentStyle.LineType = Drawing2D.DashStyle.DashDotDot
currentStyle.LineWidth = 2
Case 11
currentStyle.LineType = Drawing2D.DashStyle.Solid
currentStyle.LineWidth = 3
Case 12
currentStyle.LineType = Drawing2D.DashStyle.Dash
currentStyle.LineWidth = 3
Case 13
currentStyle.LineType = Drawing2D.DashStyle.Dot
currentStyle.LineWidth = 3
Case 14
currentStyle.LineType = Drawing2D.DashStyle.DashDot
currentStyle.LineWidth = 3
Case 15
currentStyle.LineType = Drawing2D.DashStyle.DashDotDot
currentStyle.LineWidth = 3
End Select
'set current style
Me.LineStyle = currentStyle
'Debug.WriteLine(Me.SelectedIndex)
'Debug.WriteLine(Me.LineStyle.LineType)
'Debug.WriteLine(Me.LineStyle.LineWidth)
End Sub
Public Sub DrawColorStyle()Sub DrawColorStyle(ByVal mycolor As System.Drawing.Color, ByVal e As DrawItemEventArgs)
End Sub
#End Region
End Class
Inherits System.Windows.Forms.ListBox
LineStyle Structure >>#Region "LineStyle Structure >>"
'line type:
' none(0)----->Custom
' (1)---->Solid
' (2)---->Dash
' (3)---->Dot
' (4)---->DashDot
' (5)---->DashDotDot
Public Structure StyleStructure Style
Public LineType As System.Drawing.Drawing2D.DashStyle
Public LineWidth As Single
'Public LineColor As System.Drawing.Color
End Structure
#End Region
Property>>#Region "Property>>"
'current line style(0--15)
Private _lineStyle As Style
Public Property LineStyle()Property LineStyle() As Style
Get
Return _lineStyle
End Get
Set(ByVal Value As Style)
_lineStyle = Value
End Set
End Property
'current line color
Private _lineColor As Color
Public Property LineColor()Property LineColor() As Color
Get
If _lineColor.Equals(Color.Empty) Then
_lineColor = Color.Black
End If
Return _lineColor
End Get
Set(ByVal Value As Color)
_lineColor = Value
End Set
End Property
Private _alternate As Integer = 5
#End Region
Subs/Functions>>#Region "Subs/Functions>>"
'draw the specified line in listbox
Protected Overrides Sub OnDrawItem()Sub OnDrawItem(ByVal e As DrawItemEventArgs)
MyBase.OnDrawItem(e)
Me.DrawMode = DrawMode.OwnerDrawFixed
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
Dim selectBrush As New SolidBrush(Me.BackColor)
e.Graphics.FillRectangle(selectBrush, e.Bounds)
Dim selectPen As New Pen(Color.Gray)
selectPen.DashStyle = Drawing2D.DashStyle.Dash
e.Graphics.DrawRectangle(selectPen, e.Bounds)
Else
Dim selectBrush As New SolidBrush(Me.BackColor)
e.Graphics.FillRectangle(selectBrush, e.Bounds)
Dim selectPen As New Pen(Me.BackColor)
e.Graphics.DrawRectangle(selectPen, e.Bounds)
End If
' e.DrawBackground()
e.DrawFocusRectangle()
Dim myPen As New Pen(Me.LineColor)
' Dim myPen As New Pen(Color.Red)
Select Case e.Index
Case 0
'none
Case 1
'solid
myPen.DashStyle = Drawing2D.DashStyle.Solid
Case 2
'dash
myPen.DashStyle = Drawing2D.DashStyle.Dash
Case 3
'dot
myPen.DashStyle = Drawing2D.DashStyle.Dot
Case 4
'dashdot
myPen.DashStyle = Drawing2D.DashStyle.DashDot
Case 5
'dashdotdot
myPen.DashStyle = Drawing2D.DashStyle.DashDotDot
Case 6
'solid
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.Solid
Case 7
'dash
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.Dash
Case 8
'dot
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.Dot
Case 9
'dashdot
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.DashDot
Case 10
'dashdotdot
myPen.Width = 2
myPen.DashStyle = Drawing2D.DashStyle.DashDotDot
Case 11
'solid
myPen.Width = 3
myPen.DashStyle = Drawing2D.DashStyle.Solid
'Case 12
' 'dash
' myPen.Width = 3
' myPen.DashStyle = Drawing2D.DashStyle.Dash
'Case 13
' 'dot
' myPen.Width = 3
' myPen.DashStyle = Drawing2D.DashStyle.Dot
'Case 14
' 'dashdot
' myPen.Width = 3
' myPen.DashStyle = Drawing2D.DashStyle.DashDot
'Case 15
' 'dashdotdot
' myPen.Width = 3
' myPen.DashStyle = Drawing2D.DashStyle.DashDotDot
End Select
'e.Graphics.DrawRectangle(myPen, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
If e.Index = 0 Then
Dim myBrush As New SolidBrush(Color.Black)
e.Graphics.DrawString(Me.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y + _alternate, e.Bounds.Width, e.Bounds.Height))
Else
e.Graphics.DrawLine(myPen, e.Bounds.X + _alternate, e.Bounds.Y + CInt(e.Bounds.Height / 2), e.Bounds.X + e.Bounds.Width - _alternate, e.Bounds.Y + CInt(e.Bounds.Height / 2))
End If
End Sub
'set current selectedindex
Private Sub ListStyle_SelectedIndexChanged()Sub ListStyle_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SelectedIndexChanged
Dim currentStyle As Style
Select Case Me.SelectedIndex
Case 0
currentStyle.LineType = Drawing2D.DashStyle.Custom 'represent for none preset
currentStyle.LineWidth = 1
Case 1
currentStyle.LineType = Drawing2D.DashStyle.Solid
currentStyle.LineWidth = 1
Case 2
currentStyle.LineType = Drawing2D.DashStyle.Dash
currentStyle.LineWidth = 1
Case 3
currentStyle.LineType = Drawing2D.DashStyle.Dot
currentStyle.LineWidth = 1
Case 4
currentStyle.LineType = Drawing2D.DashStyle.DashDot
currentStyle.LineWidth = 1
Case 5
currentStyle.LineType = Drawing2D.DashStyle.DashDotDot
currentStyle.LineWidth = 1
Case 6
currentStyle.LineType = Drawing2D.DashStyle.Solid
currentStyle.LineWidth = 2
Case 7
currentStyle.LineType = Drawing2D.DashStyle.Dash
currentStyle.LineWidth = 2
Case 8
currentStyle.LineType = Drawing2D.DashStyle.Dot
currentStyle.LineWidth = 2
Case 9
currentStyle.LineType = Drawing2D.DashStyle.DashDot
currentStyle.LineWidth = 2
Case 10
currentStyle.LineType = Drawing2D.DashStyle.DashDotDot
currentStyle.LineWidth = 2
Case 11
currentStyle.LineType = Drawing2D.DashStyle.Solid
currentStyle.LineWidth = 3
Case 12
currentStyle.LineType = Drawing2D.DashStyle.Dash
currentStyle.LineWidth = 3
Case 13
currentStyle.LineType = Drawing2D.DashStyle.Dot
currentStyle.LineWidth = 3
Case 14
currentStyle.LineType = Drawing2D.DashStyle.DashDot
currentStyle.LineWidth = 3
Case 15
currentStyle.LineType = Drawing2D.DashStyle.DashDotDot
currentStyle.LineWidth = 3
End Select
'set current style
Me.LineStyle = currentStyle
'Debug.WriteLine(Me.SelectedIndex)
'Debug.WriteLine(Me.LineStyle.LineType)
'Debug.WriteLine(Me.LineStyle.LineWidth)
End Sub
Public Sub DrawColorStyle()Sub DrawColorStyle(ByVal mycolor As System.Drawing.Color, ByVal e As DrawItemEventArgs)
End Sub
#End Region
End Class
snapshoot: