一个自定义的Combox

来源:互联网 发布:网站怎么优化排名靠前 编辑:程序博客网 时间:2024/05/01 16:12

如何在windows标准的combox中加入自定义的item

 

Public Class ComboPattern
    
Inherits System.Windows.Forms.ComboBox

Component Designer generated code

    
'get or set current pattern style
    Private _patternstyle As System.Drawing.Drawing2D.HatchStyle
    
Public 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



    
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.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 myBrush As System.Drawing.Drawing2D.HatchBrush

        
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)

            
Case 3
                
'DarkDownwardDiagonal
                myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DarkDownwardDiagonal, Color.Black, Me.BackColor)

            
Case 4
                
'DarkHorizontal
                myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DarkHorizontal, Color.Black, Me.BackColor)

            
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)

            
Case 8
                
'DashedHorizontal
                myBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing2D.HatchStyle.DashedHorizontal, Color.Black, Me.BackColor)

            
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)

        
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 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)

        
End If


    
End Sub



    
Private Sub ComboPattern_SelectedIndexChanged(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.SelectedIndexChanged
        
If (Me.DropDownStyle <> ComboBoxStyle.DropDownList) Then
            
Me.DropDownStyle = ComboBoxStyle.DropDownList
        
End If

        
Dim currentPattern As System.Drawing.Drawing2D.HatchStyle

        
Select Case Me.SelectedIndex
            
Case 0
                
'no pattern, use ZigZag to instead of
                currentPattern = Drawing2D.HatchStyle.ZigZag

            
Case 1
                
'BackwardDiagonal
                currentPattern = Drawing2D.HatchStyle.BackwardDiagonal
            
Case 2
                
'Cross
                currentPattern = Drawing2D.HatchStyle.Cross

            
Case 3
                
'DarkDownwardDiagonal
                currentPattern = Drawing2D.HatchStyle.DarkDownwardDiagonal

            
Case 4
                
'DarkHorizontal
                currentPattern = Drawing2D.HatchStyle.DarkHorizontal

            
Case 5
                
'DarkUpwardDiagonal
                currentPattern = Drawing2D.HatchStyle.DarkUpwardDiagonal

            
Case 6
                
'DarkVertical
                currentPattern = Drawing2D.HatchStyle.DarkVertical

            
Case 7
                
'DashedDownwardDiagonal
                currentPattern = Drawing2D.HatchStyle.DashedDownwardDiagonal


            
Case 8
                
'DashedHorizontal
                currentPattern = Drawing2D.HatchStyle.DarkHorizontal

            
Case 9
                
'DashedUpwardDiagonal
                currentPattern = Drawing2D.HatchStyle.DashedUpwardDiagonal

            
Case 10
                
'DashedVertical
                currentPattern = Drawing2D.HatchStyle.DashedVertical
        
End Select
        
Me.PatternStyle = currentPattern
    
End Sub





End Class

snapshot:

原创粉丝点击