'显示的项数
Public Property MaxDropDownItems() As Integer
Get
MaxDropDownItems = Me.CBox.MaxDropDownItems
End Get
Set(ByVal Value As Integer)
Me.CBox.MaxDropDownItems = Value
End Set
End Property
'列数
Public Property ColumnCount() As Integer
Get
ColumnCount = CCount
End Get
Set(ByVal Value As Integer)
If Value < 1 Then
MsgBox("属性值无效!", 16, "错误")
Else
CCount = Value
End If
End Set
End Property
'绑定列
Public Property BoundColumn() As Integer
Get
BoundColumn = BColumn
End Get
Set(ByVal Value As Integer)
If Value < 1 Then
MsgBox("属性值无效!", 16, "错误")
Else
BColumn = Value
End If
End Set
End Property
'下拉按钮
Public Property DownButton() As Boolean
Get
DownButton = DButton
End Get
Set(ByVal Value As Boolean)
DButton = Value
If Value = True Then
Me.CBox.Width = Me.PBox.Width + 4
Else
Me.CBox.Width = Me.PBox.Width + 20
End If
End Set
End Property
'限于列表
Public Property LimitList() As Boolean
Get
LimitList = LList
End Get
Set(ByVal Value As Boolean)
LList = Value
End Set
End Property
Function ComboBoxItem() As Boolean
If LimitList = False Then
ComboBoxItem = True
Exit Function
End If
If Len(Trim(Me.CBox.Text)) = 0 Then
ComboBoxItem = True
Exit Function
End If
NewText()
Dim i As Integer
For i = 0 To CBox.Items.Count - 1
If UCase(Trim(Me.CBox.Text)) = UCase(Trim(GetString(CBox.Items.Item(i).ToString))) Then
ComboBoxItem = True
Exit For
Exit Function
End If
Next
If i = CBox.Items.Count Then
MsgBox("输入的值不在列表中。", , "提示")
'CBox.DroppedDown = True
ComboBoxItem = False
End If
End Function
Function GetString(ByVal str As String) As String
If Len(Trim(str)) = 0 Then Exit Function
If InStr(str, "|", CompareMethod.Text) = 0 Then
GetString = Trim(str)
Exit Function
End If
Dim a As Integer
For i = 1 To Len(str)
If GetChar(str, i) = "|" Then
a = a + 1
If a = BoundColumn - 1 Then
str = Mid(str, i + 1)
Exit For
End If
End If
Next
GetString = Mid(str, 1, InStr(str, "|", CompareMethod.Text) - 1)
End Function
Sub NewText()
If InStr(Me.CBox.Text, "|", CompareMethod.Text) > 0 Then
Me.CBox.Text = Trim(GetString(Me.CBox.Text))
End If
End Sub
Private Sub CBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.SelectedValueChanged
Dim T1 As Thread = New Thread(AddressOf NewText)
T1.Start()
End Sub
Private Sub CBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.Leave
Dim T1 As Thread = New Thread(AddressOf NewText)
T1.Start()
If ComboBoxItem() = False Then
Me.CBox.Focus()
Else
RaiseEvent LostFocus(sender, e)
End If
End Sub
'回车自动跳出
Private Sub CBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CBox.KeyPress
If e.KeyChar.GetHashCode = 851981 Then
If EnterAsTab = True Then SendKeys.SendWait("{TAB}")
Else
If BoundColumn = 1 Then
If Me.CBox.Items.Count > 0 Then Me.CBox.DroppedDown = True
End If
End If
End Sub
Private Sub CBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.Enter
If Len(Trim(CBox.Text)) = 0 Or (BoundColumn = 1 And Len(Trim(CBox.Text)) = 0) Then
If Me.CBox.Items.Count > 0 Then Me.CBox.DroppedDown = True
End If
End Sub
Private Sub CBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.Click
If Me.CBox.Items.Count > 0 Then Me.CBox.DroppedDown = True
NewText()
End Sub
Private Sub CBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.TextChanged
RaiseEvent TextChanged(sender, e)
End Sub
Private Sub CBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.LostFocus
If LimitList = False Then RaiseEvent LostFocus(sender, e)
End Sub
Private Sub CBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.GotFocus
RaiseEvent GotFocus(sender, e)
End Sub
End Class
Private Sub TextBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
Me.Height = Me.CBox.Height - 3
End Sub
Public Overrides Property Text() As String
Get
Text = Me.CBox.Text
End Get
Set(ByVal Value As String)
Me.CBox.Text = Value
End Set
End Property
'背景颜色
Public Property CombBoxBackColor() As Color
Get
CombBoxBackColor = Me.CBox.BackColor
End Get
Set(ByVal Value As Color)
Me.CBox.BackColor = Value
End Set
End Property
'前景颜色
Public Property CombBoxForeColor() As Color
Get
CombBoxForeColor = Me.CBox.ForeColor
End Get
Set(ByVal Value As Color)
Me.CBox.ForeColor = Value
End Set
End Property
'是否可用
Public Property CombBoxEnabled() As Boolean
Get
CombBoxEnabled = Me.PBox.Enabled
End Get
Set(ByVal Value As Boolean)
If Value = False Then
Me.BackColor = System.Drawing.Color.LightSteelBlue
Else
Me.BackColor = System.Drawing.Color.CornflowerBlue
End If
Me.PBox.Enabled = Value
End Set
End Property
'回车 as Tab
Public Property EnterAsTab() As Boolean
Get
EnterAsTab = EnterTab
End Get
Set(ByVal Value As Boolean)
EnterTab = Value
End Set
End Property
'下拉列表行来源
Public Property RowSource() As String
Get
RowSource = RSource
End Get
Set(ByVal Value As String)
RSource = Trim(Value)
RS()
End Set
End Property
Sub RS()
On Error Resume Next
Me.CBox.Items.Clear()
If Len(RSource) = 0 Then Exit Sub
If Microsoft.VisualBasic.Right(RSource, 1) <> ";" Then RSource = RSource & ";"
Dim ColumnW() As String
ReDim ColumnW(StrCount(ColumnWidth, ",") - 1)
ColumnW = Split(ColumnWidth, ",")
Dim RValue() As String
ReDim RValue(StrCount(RSource, ";") - 1)
RValue = Split(RSource, ";")
For i = 0 To UBound(RValue) - 1
Dim CValue() As String
ReDim CValue(StrCount(RValue(i), "'") - 1)
CValue = Split(RValue(i), "'")
Dim txt As String = ""
For x = 0 To ColumnCount - 1
Dim tmpS As Integer
If x > UBound(ColumnW) - 1 Then
tmpS = 10
Else
tmpS = CInt(Mid(ColumnW(x), 1, Len(ColumnW(x)) - 2))
End If
If LenChar(CValue(0)) = 0 Then CValue(0) = "." 'StrDup(tmpS, Chr(32)) ' Space(tmpS)
Dim Ctxt As String = IIf(x > UBound(RValue), "", CValue(x))
Dim S As String
If LenChar(Ctxt) >= tmpS Then
S = ""
Else
S = Space(tmpS - LenChar(Ctxt))
End If
txt = txt & IIf(LenChar(Ctxt) <= tmpS, Ctxt, Mid(Ctxt, 1, tmpS)) & S & "|"
Next
Me.CBox.Items.Add(Trim(Mid(txt, 1, Len(txt) - 1)))
Next
End Sub
'下拉框宽度
Public Property DropDownWidth() As Integer
Get
DropDownWidth = Me.CBox.DropDownWidth
End Get
Set(ByVal Value As Integer)
Me.CBox.DropDownWidth = Value
End Set
End Property
'列宽度
Public Property ColumnWidth() As String
Get
ColumnWidth = CWidth
End Get
Set(ByVal Value As String)
Value = Replace(Value, " ", "")
For i = 1 To Len(Value)
Dim v As String = GetChar(Value, i)
If (v = "0" Or v = "1" Or v = "2" Or v = "3" Or v = "4" Or v = "5" Or v = "6" Or v = "7" Or v = "8" Or v = "9" Or v = "字" Or v = "节" Or v = ",") = False Then
MsgBox("属性值无效!", 16, "错误")
Exit Property
End If
Next
Dim ColumnW() As String
ReDim ColumnW(StrCount(Value, ",") - 1)
ColumnW = Split(Value, ",")
For i = 0 To UBound(ColumnW) - 1
If Len(ColumnW(i)) >= 3 Then
If Mid(ColumnW(i), Len(ColumnW(i)) - 1) <> "字节" Then
ColumnW(i) = ColumnW(i) & "字节"
End If
Else
ColumnW(i) = ColumnW(i) & "字节"
End If
Next
Value = ""
For i = 0 To UBound(ColumnW) - 1
Value = Value & ColumnW(i) & ","
Next
CWidth = Value
RS()
End Set
End Property
Imports System.Threading
Public Class ComboBox
Inherits System.Windows.Forms.UserControl
Shadows Event TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Shadows Event LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
Shadows Event GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
Dim i As Integer, x As Integer
Dim EnterTab As Boolean = True
Dim RSource As String
Dim CCount As Integer = 1
Dim BColumn As Integer = 1
Dim CWidth As String
Dim LList As Boolean
Dim DButton As Boolean
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'UserControl 重写 dispose 以清理组件列表。
Protected Overloads Overrides 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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents CBox As System.Windows.Forms.ComboBox
Friend WithEvents PBox As System.Windows.Forms.Panel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.CBox = New System.Windows.Forms.ComboBox
Me.PBox = New System.Windows.Forms.Panel
Me.PBox.SuspendLayout()
Me.SuspendLayout()
'
'CBox
'
Me.CBox.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.CBox.BackColor = System.Drawing.Color.White
Me.CBox.ForeColor = System.Drawing.Color.Navy
Me.CBox.ItemHeight = 12
Me.CBox.Location = New System.Drawing.Point(-2, -2)
Me.CBox.Name = "CBox"
Me.CBox.Size = New System.Drawing.Size(106, 20)
Me.CBox.TabIndex = 1
'
'PBox
'
Me.PBox.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.PBox.Controls.Add(Me.CBox)
Me.PBox.Location = New System.Drawing.Point(1, 1)
Me.PBox.Name = "PBox"
Me.PBox.Size = New System.Drawing.Size(86, 15)
Me.PBox.TabIndex = 2
'
'ComboBox
'
Me.BackColor = System.Drawing.Color.CornflowerBlue
Me.Controls.Add(Me.PBox)
Me.Name = "ComboBox"
Me.Size = New System.Drawing.Size(88, 17)
Me.PBox.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub TextBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox_Resize(sender, e)
RS()
'addhandler CBox.SizeChanged
End Sub