一个自定义的ListBox
来源:互联网 发布:linux常用命令jar 编辑:程序博客网 时间:2024/05/07 16:20
如何在标准的ListBox中增加自定义的Item
Public Class ListStyleClass 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:
- 一个自定义的ListBox
- Listbox的一个用法
- Silverlight自定义漂亮的ListBox
- 自定义的 ListBoxItem 自适应ListBox的宽度
- 带图标和自定义颜色的ListBox
- 带图标和自定义颜色的ListBox
- 自定义WPF ListBox的选择样式
- 带图标和自定义颜色的ListBox
- WPF 自定义listbox的ItemTemplate实现通讯录
- 一个非常Strong 的ListBox控件EasyList
- 自定义控件之ListBox
- listBox自定义颜色字体
- Silverlight 中 自定义的 ListBoxItem如何自适应ListBox的宽度
- 带图标和自定义颜色的ListBox yzx110 [原作]
- 两个listbox之间的互换(自定义控件)
- C#实现item自定义颜色的ListBox,显示日志
- 获得VB listbox 选中项目(多项)的一个好方法
- 修复WPF Silverlight ListBox控件的一个Bug
- 2007经典妙语
- Eclipse+MyEclipse+Tomcat的配置
- CSS属性Display与Visibility不同(转)
- 使用 OProfile for Linux on POWER 识别性能瓶颈
- ORACLE常用网址
- 一个自定义的ListBox
- 请问C#中get和SET到底是什么意思啊?
- 站点地图(3)
- js控制图片显示的大小
- 如果监控Spring Bean生命周期
- 如何调试用于PPC设备的dll
- native2ascii的用法
- PPC U-Boot随读随写(1)
- C#操作注册表及设置开机自动运行