CheckedCombobox模拟

来源:互联网 发布:python自学书籍 编辑:程序博客网 时间:2024/05/06 19:32

CheckedCombobox模拟

'文件列表:
'CheckedCombobox.Designer.vb
' CheckedCombobox.vb

'
'CheckedCombobox.Designer.vb
'

 Partial Class CheckedCombobox
    Inherits System.Windows.Forms.TextBox

    <System.Diagnostics.DebuggerNonUserCode()> _
    Public Sub New(ByVal Container As System.ComponentModel.IContainer)
        MyClass.New()

        'Windows.Forms 类撰写设计器支持所必需的
        Container.Add(Me)

    End Sub

    <System.Diagnostics.DebuggerNonUserCode()> _
    Public Sub New()
        MyBase.New()


        '组件设计器需要此调用。
        InitializeComponent()
        '自定义
        Me.ReadOnly = True
        Me.BackColor = Color.White
        '
        Me.Controls.Add(Me.Button1)
        Me.Button1.Left = Me.Width - Me.Button1.Width - 3
        Me.Button1.Top = Me.Top - 1
        '
        Me.Cursor = Cursors.Arrow
    End Sub

    'Component 重写 Dispose,以清理组件列表。
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    '组件设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是组件设计器所必需的
    '可使用组件设计器修改它。
    '不要使用代码编辑器修改它。
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(CheckedCombobox))
        Me.Button1 = New System.Windows.Forms.Button
        Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Dock = System.Windows.Forms.DockStyle.Right
        Me.Button1.Image = CType(resources.GetObject("Button1.Image"), System.Drawing.Image)
        Me.Button1.Location = New System.Drawing.Point(100, 0)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(17, 17)
        Me.Button1.TabIndex = 4
        Me.Button1.UseVisualStyleBackColor = True
        '
        'CheckedListBox1
        '
        Me.CheckedListBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.CheckedListBox1.CheckOnClick = True
        Me.CheckedListBox1.FormattingEnabled = True
        Me.CheckedListBox1.Location = New System.Drawing.Point(0, 0)
        Me.CheckedListBox1.Name = "CheckedListBox1"
        Me.CheckedListBox1.Size = New System.Drawing.Size(120, 96)
        Me.CheckedListBox1.TabIndex = 0
        '
        'CheckedCombobox
        '
        Me.Controls.Add(Me.Button1)
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox
    Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip

End Class 

 

'
' CheckedCombobox.vb
'

Public Class CheckedCombobox
    Inherits TextBox

#Region "控件属性"
    Private _SpiltChar As String = ","           '分隔字符
    Private _DroppedDown As Boolean = False      '下拉部份是否显示

    Private _DropDownWidth As Integer = 172      '下拉部份的 宽度
    Private _DropDownHeight As Integer = 82      '下拉部份的 高度
    Private _CheckOnClick As Boolean = False     '

    Private _Items As CheckedListBox.ObjectCollection '= Me.CheckedListBox1.Items

    Property Items() As CheckedListBox.ObjectCollection
        Get
            Return _Items
        End Get
        Set(ByVal value As CheckedListBox.ObjectCollection)
            _Items = value
        End Set
    End Property

    Property SpiltChar() As String
        Get
            Return _SpiltChar
        End Get
        Set(ByVal Value As String)
            _SpiltChar = Value
        End Set
    End Property

    Property CheckOnClick() As Boolean
        Get
            Return _CheckOnClick
        End Get
        Set(ByVal Value As Boolean)
            _CheckOnClick = Value
        End Set
    End Property

    Property DroppedDown() As Boolean
        Get
            Return _DroppedDown
        End Get
        Set(ByVal Value As Boolean)
            _DroppedDown = Value
        End Set
    End Property

    Property DropDownWidth() As Integer
        Get
            Return _DropDownWidth
        End Get
        Set(ByVal Value As Integer)
            _DropDownWidth = Value
        End Set
    End Property

    Property DropDownHeight() As Integer
        Get
            Return _DropDownHeight
        End Get
        Set(ByVal Value As Integer)
            _DropDownHeight = Value
        End Set
    End Property

#End Region

#Region "控件事件"

    Private Sub CheckedComboBox_OnDroppedDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click, Button1.Click
        If Me.DroppedDown = False Then
            If Me.Parent IsNot Nothing Then
                If Not Me.Parent.Controls.Contains(Me.CheckedListBox1) Then
                    Me.CheckedListBox1.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
                    Me.CheckedListBox1.Top = Me.Top + Me.Height
                    Me.CheckedListBox1.Left = Me.Left
                    Me.CheckedListBox1.Width = Me.DropDownWidth
                    Me.CheckedListBox1.Height = Me.DropDownHeight
                    Me.CheckedListBox1.CheckOnClick = Me.CheckOnClick
                    Me.Parent.Controls.Add(Me.CheckedListBox1)
                End If
                '
                Me.CheckedListBox1.BringToFront()
                '
                ShowDropDown(True)
                AddHandler Me.FindForm.Click, AddressOf My_From_Click
            Else
                RemoveHandler Me.FindForm.GotFocus, AddressOf My_From_Click
            End If
        Else
            ShowDropDown(False)
        End If
    End Sub

    Sub ShowDropDown(ByVal DropDown As Boolean)
        Me.CheckedListBox1.Visible = DropDown
        Me.DroppedDown = DropDown
        '
        If DroppedDown Then Me.CheckedListBox1.Focus()
        '
    End Sub

    Sub My_From_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ShowDropDown(False)
    End Sub

    Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        Me.Cursor = Cursors.Arrow
    End Sub

    Private Sub CheckedCombobox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyData = Keys.Escape Then ShowDropDown(False)
    End Sub

    Private Sub CheckedListBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CheckedListBox1.KeyDown
        If e.KeyCode = Keys.Escape Then ShowDropDown(False)
    End Sub

    Private Sub CheckedListBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.Leave
        ShowDropDown(False)
    End Sub

    Private Sub CheckedListBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.LostFocus
        ShowDropDown(False)
    End Sub
    Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
        If Me.Text.Contains(CheckedListBox1.Items(e.Index).ToString) Then
            If e.NewValue = CheckState.Unchecked Then
                Dim txtAL As ArrayList = New ArrayList
                txtAL.AddRange(Me.Text.Split(Me.SpiltChar))
                txtAL.Remove(CheckedListBox1.Items(e.Index).ToString)
                Dim txt As String = ""
                For i As Integer = 0 To txtAL.Count - 1
                    If i = txtAL.Count - 1 Then
                        txt = txt & txtAL.Item(i)
                    Else
                        txt = txt & txtAL.Item(i) & Me.SpiltChar
                    End If
                Next
                Me.Text = txt
            End If
        Else
            If Me.Text = "" Then
                Me.Text = Me.Text & CheckedListBox1.Items(e.Index).ToString
            Else
                Me.Text = Me.Text & Me.SpiltChar & CheckedListBox1.Items(e.Index).ToString
            End If
        End If
        '
        Me.ToolTip1.SetToolTip(Me, Me.Text)
    End Sub
    '
    '
    '
    Public Sub ItemAdd(ByVal Item As Object)
        Me.CheckedListBox1.Items.Add(Item)
    End Sub

    Public Sub ItemsAddRange(ByVal Items As Object())
        Me.CheckedListBox1.Items.AddRange(Items)
    End Sub

    Public Sub ItemsClear(ByVal Items As Object())
        Me.CheckedListBox1.Items.Clear()

    End Sub

#End Region


End Class