如何在windows标准的combox中加入自定义的item
Public
Class ComboPattern
Class ComboPattern
Inherits System.Windows.Forms.ComboBox
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
Component Designer generated code#Region " Component Designer generated code "
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Public Sub New()Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Public Sub New()Sub New()
MyBase.New()
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'This call is required by the Component Designer.
InitializeComponent()
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'Add any initialization after the InitializeComponent() call
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'Component overrides dispose to clean up the component list.
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#End Region
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'get or set current pattern style
Private _patternstyle As System.Drawing.Drawing2D.HatchStyle
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Public Property PatternStyle()Property PatternStyle() As System.Drawing.Drawing2D.HatchStyle
Get
Return _patternstyle
End Get
Set(ByVal Value As System.Drawing.Drawing2D.HatchStyle)
_patternstyle = Value
End Set
End Property
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Protected Overrides Sub OnDrawItem()Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
MyBase.OnDrawItem(e)
Me.DrawMode = DrawMode.OwnerDrawFixed
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
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()
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Dim myBrush As System.Drawing.Drawing2D.HatchBrush
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Select Case e.Index
Case 0
'no pattern
Case 1
'BackwardDiagonal
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, Color.Black, Me.BackColor)
Case 2
'Cross
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.Cross, Color.Black, Me.BackColor)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 3
'DarkDownwardDiagonal
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DarkDownwardDiagonal, Color.Black, Me.BackColor)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 4
'DarkHorizontal
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DarkHorizontal, Color.Black, Me.BackColor)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 5
'DarkUpwardDiagonal
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DarkUpwardDiagonal, Color.Black, Me.BackColor)
Case 6
'DarkVertical
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DarkVertical, Color.Black, Me.BackColor)
Case 7
'DashedDownwardDiagonal
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DashedDownwardDiagonal, Color.Black, Me.BackColor)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 8
'DashedHorizontal
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DashedHorizontal, Color.Black, Me.BackColor)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 9
'DashedUpwardDiagonal
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DashedUpwardDiagonal, Color.Black, Me.BackColor)
Case 10
'DashedVertical
myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DashedVertical, Color.Black, Me.BackColor)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Select
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
'e.Graphics.DrawRectangle(myPen, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
If e.Index = 0 Then
Dim myBrush1 As New SolidBrush(Color.Black)
e.Graphics.DrawString(Me.Items(e.Index), e.Font, myBrush1, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
Else
'e.Graphics.DrawLine(myPen, e.Bounds.X, e.Bounds.Y + CInt(e.Bounds.Height / 2), e.Bounds.X + e.Bounds.Width, e.Bounds.Y + CInt(e.Bounds.Height / 2))
e.Graphics.FillRectangle(myBrush, e.Bounds)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End If
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
Private Sub ComboPattern_SelectedIndexChanged()Sub ComboPattern_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SelectedIndexChanged
If (Me.DropDownStyle <> ComboBoxStyle.DropDownList) Then
Me.DropDownStyle = ComboBoxStyle.DropDownList
End If
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Dim currentPattern As System.Drawing.Drawing2D.HatchStyle
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Select Case Me.SelectedIndex
Case 0
'no pattern, use ZigZag to instead of
currentPattern = Drawing2D.HatchStyle.ZigZag
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 1
'BackwardDiagonal
currentPattern = Drawing2D.HatchStyle.BackwardDiagonal
Case 2
'Cross
currentPattern = Drawing2D.HatchStyle.Cross
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 3
'DarkDownwardDiagonal
currentPattern = Drawing2D.HatchStyle.DarkDownwardDiagonal
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 4
'DarkHorizontal
currentPattern = Drawing2D.HatchStyle.DarkHorizontal
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 5
'DarkUpwardDiagonal
currentPattern = Drawing2D.HatchStyle.DarkUpwardDiagonal
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 6
'DarkVertical
currentPattern = Drawing2D.HatchStyle.DarkVertical
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 7
'DashedDownwardDiagonal
currentPattern = Drawing2D.HatchStyle.DashedDownwardDiagonal
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 8
'DashedHorizontal
currentPattern = Drawing2D.HatchStyle.DarkHorizontal
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 9
'DashedUpwardDiagonal
currentPattern = Drawing2D.HatchStyle.DashedUpwardDiagonal
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Case 10
'DashedVertical
currentPattern = Drawing2D.HatchStyle.DashedVertical
End Select
Me.PatternStyle = currentPattern
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
End Class
snapshot:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/cooleader320/MyCombo.png)