麻烦看下,代码哪里有问题。
multicolumnComboBox的代码,抄的,原来的c的,改了vb,完全没有什么效果啊。Imports System
Imports System.Windows.Forms
Imports System.Collections
Imports System.Collections.ObjectModel
Imports System.Drawing
Imports System.Globalization
Public Class MultiColumnComboBox
Inherits System.Windows.Forms.ComboBox
Private _AutoComplete As Boolean
Private _AutoDropdown As Boolean
Private _BackColorEven As Color = Color.White
Private _BackColorOdd As Color = Color.White
Private _ColumnNameString As String = ""
Private _ColumnWidthDefault As Integer = 75
Private _ColumnWidthString As String = ""
Private _LinkedColumnIndex As Int32
Private _LinkedTextBox As System.Windows.Forms.TextBox
Private _TotalWidth As Int32
Private _ValueMemberColumnIndex As Int32
Private _ColumnNames As New Collection(Of String)
Private _ColumnWidths As New Collection(Of Int32)
Public Sub MultiColumnComboBox()
DrawMode = DrawMode.OwnerDrawVariable
ContextMenu = New ContextMenu()
End Sub
Public Event OpenSearchForm As System.EventHandler
Public Property AutoComplete() As Boolean
Get
Return _AutoComplete
End Get
Set(ByVal value As Boolean)
_AutoComplete = value
End Set
End Property
Public Property AutoDropdown As Boolean
Get
Return _AutoDropdown
End Get
Set(ByVal value As Boolean)
_AutoDropdown = value
End Set
End Property
Public Property BackColorEven As Color
Get
Return _BackColorEven
End Get
Set(ByVal value As Color)
_BackColorEven = value
End Set
End Property
Public Property BackColorOdd As Color
Get
Return _BackColorOdd
End Get
Set(ByVal value As Color)
_BackColorOdd = value
End Set
End Property
Public ReadOnly Property ColumnNameCollection As Collection(Of String)
Get
Return _ColumnNames
End Get
End Property
--------------------编程问答--------------------
Public ReadOnly Property ColumnWidthCollection As Collection(Of Int32)--------------------编程问答--------------------
Get
Return _ColumnWidths
End Get
End Property
Public Property ColumnNames As String
Get
Return _ColumnNameString
End Get
Set(ByVal value As String)
If Not Convert.ToBoolean(value.Trim().Length) Then
_ColumnNameString = ""
ElseIf value IsNot Nothing Then
Dim delimiterChars() As Char = {",", ";", ":"}
Dim columnNames = value.Split(delimiterChars)
If Not DesignMode Then
_ColumnNames.Clear()
End If
For Each s As String In columnNames
If Convert.ToBoolean(s.Trim().Length) Then
If Not DesignMode Then
_ColumnNames.Add(s.Trim())
End If
Else
Throw New NotSupportedException("Column names can not be blank")
End If
Next
_ColumnNameString = value
End If
End Set
End Property
Public Property columnWidthDefault As Int32
Get
Return _ColumnWidthDefault
End Get
Set(ByVal value As Int32)
_ColumnWidthDefault = value
End Set
End Property
Public Property ColumnWidths As String
Get
Return _ColumnWidthString
End Get
Set(ByVal value As String)
If Not Convert.ToBoolean(value.Trim().Length) Then
_ColumnWidthString = ""
ElseIf value IsNot Nothing Then
Dim delimiterChars() As Char = {",", ";", ":"}
Dim columnWdiths = value.Split(delimiterChars)
Dim invalidValue As String = ""
Dim invalidIndex As Int32 = -1
Dim idx As Int32 = 1
Dim intValue As Int32
For Each s As String In ColumnWidths
If Convert.ToBoolean(s.Trim().Length) Then
If Not Int32.TryParse(s, intValue) Then
invalidIndex = idx
invalidValue = s
Else
idx = idx + 1
End If
Else
idx = idx + 1
End If
Next
If invalidIndex > -1 Then
Dim errMsg As String = "Invalid column width '" + invalidValue + "' located at column " + invalidIndex.ToString()
Throw New ArgumentOutOfRangeException(errMsg)
Else
_ColumnWidthString = value
If Not DesignMode Then
_ColumnWidths.Clear()
For Each s As String In columnWdiths
If Convert.ToBoolean(s.Trim().Length) Then
_ColumnWidths.Add(Convert.ToInt32(s))
Else
_ColumnWidths.Add(_ColumnWidthDefault)
End If
Next
If DataManager IsNot Nothing Then initializeColumns()
End If
End If
End If
End Set
End Property
Public Overloads Property DrawMode As DrawMode
Get
Return MyBase.DrawMode
End Get
Set(ByVal value As DrawMode)
If value <> DrawMode.OwnerDrawVariable Then
Throw New NotSupportedException("Needs to be DrawMode.OwnerDrawVariable")
End If
MyBase .DrawMode =value
End Set
End Property
Public Overloads Property DropDownStyle As ComboBoxStyle
Get
Return MyBase.DropDownStyle
End Get
Set(ByVal value As ComboBoxStyle)
If value <> ComboBoxStyle.DropDown Then
Throw New NotSupportedException("ComboBoxStyle.DropDown is the only supported style")
End If
MyBase.DropDownStyle = value
End Set
End Property
Public Property LinkedColumnIndex As Integer
Get
Return _LinkedColumnIndex
End Get
Set(ByVal value As Integer)
If value < 0 Then
Throw New ArgumentOutOfRangeException("A column index can not be negative")
End If
_LinkedColumnIndex = value
End Set
End Property
Public Overloads Property LinkedTextBox As TextBox
Get
Return _LinkedTextBox
End Get
Set(ByVal value As TextBox)
_LinkedTextBox = value
If _LinkedTextBox IsNot Nothing Then
_LinkedTextBox.ReadOnly = True
_LinkedTextBox.TabStop = False
End If
End Set
End Property
Public ReadOnly Property TotalWidth As Int32
Get
Return _TotalWidth
End Get
End Property
Protected Overrides Sub OnDataSourceChanged(ByVal e As System.EventArgs)
MyBase.OnDataSourceChanged(e)
initializecolumns()
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)--------------------编程问答-------------------- 窗体代码
MyBase.OnDrawItem(e)
If DesignMode Then Return
e.DrawBackground()
Dim boundsRect As Rectangle = e.Bounds
Dim lastRight As Int32 = 0
Dim brushForeColor As Color
If (e.State & DrawItemState.Selected) = 0 Then
Dim backColor As Color
backColor = IIf(Convert.ToBoolean(e.Index Mod 2), _BackColorOdd, _BackColorEven)
Using brushBackColor As SolidBrush = New SolidBrush(backColor)
e.Graphics.FillRectangle(brushBackColor, e.Bounds)
End Using
brushForeColor = Color.Black
Else
brushForeColor = Color.White
End If
Using linePen As Pen = New Pen(Color.Gray)
Using Brush As SolidBrush = New SolidBrush(brushForeColor)
If Not Convert.ToBoolean(_ColumnNames.Count) Then
e.Graphics.DrawString(Convert.ToString(Items(e.Index)), Font, Brush, boundsRect)
Else
If RightToLeft.Equals(RightToLeft.Yes) Then
Dim rtl As StringFormat = New StringFormat
rtl.Alignment = StringAlignment.Near
rtl.FormatFlags = StringFormatFlags.DirectionRightToLeft
For colIndex As Int32 = _ColumnNames.Count - 1 To 0 Step 1
If Convert.ToBoolean(_ColumnWidths(colIndex)) Then
Dim item As String = Convert.ToString(FilterItemOnProperty(Items(e.Index), _ColumnNames(colIndex)))
boundsRect.X = lastRight
boundsRect.Width = _ColumnWidths(colIndex)
lastRight = boundsRect.Right
e.Graphics.DrawString(item, Font, Brush, boundsRect, rtl)
If colIndex > 0 Then
e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom)
End If
End If
Next
Else
For colindex As Int32 = 0 To _ColumnWidths.Count - 1 Step 1
If Convert.ToBoolean(_ColumnWidths(colindex)) Then
Dim item As String = Convert.ToString(FilterItemOnProperty(Items(e.Index), _ColumnNames(colindex)))
boundsRect.X = lastRight
boundsRect.Width = _ColumnWidths(colindex)
lastRight = boundsRect.Right
e.Graphics.DrawString(item, Font, Brush, boundsRect)
If colindex < _ColumnNames.Count - 1 Then
e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom)
End If
End If
Next
End If
End If
End Using
End Using
e.DrawFocusRectangle()
End Sub
Protected Overrides Sub OnDropDown(ByVal e As System.EventArgs)
MyBase.OnDropDown(e)
If _TotalWidth > 0 Then
If (Items.Count > MaxDropDownItems) Then
Me.DropDownWidth = _TotalWidth + SystemInformation.VerticalScrollBarWidth
Else
Me.DropDownWidth = _TotalWidth
End If
End If
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
If e.KeyCode = Keys.Delete OrElse e.KeyCode = Keys.Escape Then
SelectedIndex = -1
Text = ""
If _LinkedTextBox IsNot Nothing Then
_LinkedTextBox.Text = ""
End If
'ElseIf e.KeyCode = Keys.F3 Then
' If OpenSearchForm IsNot Nothing Then
'OpenSearchForm(Me, System.EventArgs.Empty)
'End If
End If
End Sub
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
MyBase.OnKeyPress(e)
Dim idx As Int32 = -1
Dim toFind As String
DroppedDown = _AutoDropdown
If (Not Char.IsControl(e.KeyChar)) Then
If _AutoComplete Then
toFind = Text.Substring(0, SelectionStart) + e.KeyChar
idx = FindStringExact(toFind)
If idx = -1 Then
idx = FindString(toFind)
Else
DroppedDown = False
End If
If idx <> -1 Then
SelectedIndex = idx
SelectionStart = toFind.Length
SelectionLength = Text.Length - SelectionStart
Else
e.KeyChar = ChrW(0)
End If
Else
idx = FindString(e.KeyChar.ToString, SelectedIndex)
If idx <> -1 Then
SelectedIndex = idx
End If
End If
If e.KeyChar = ChrW(Keys.Back) AndAlso _AutoComplete AndAlso Convert.ToBoolean(SelectionStart) Then
toFind = Text.Substring(0, SelectionStart - 1)
idx = FindString(toFind)
If idx <> -1 Then
SelectedIndex = idx
SelectionStart = toFind.Length
SelectionLength = Text.Length - SelectionStart
End If
End If
End If
e.Handled = True
End Sub
Protected Overrides Sub OnSelectedValueChanged(ByVal e As System.EventArgs)
MyBase.OnSelectedValueChanged(e)
If _LinkedTextBox IsNot Nothing Then
If _LinkedColumnIndex < _ColumnNames.Count Then
_LinkedTextBox.Text = Convert.ToString(FilterItemOnProperty(SelectedItem, _ColumnNames(_LinkedColumnIndex)))
End If
End If
End Sub
Protected Overrides Sub OnValueMemberChanged(ByVal e As System.EventArgs)
MyBase.OnValueMemberChanged(e)
initializevaluemembercolumn()
End Sub
Private Sub InitializeColumns()
If Not Convert.ToBoolean(_ColumnNameString.Length) Then
Dim propertyDescriptorCollection As System.ComponentModel.PropertyDescriptorCollection = DataManager.GetItemProperties
_TotalWidth = 0
_ColumnNames.Clear()
For colindex As Int32 = 0 To propertyDescriptorCollection.Count - 1 Step 1
_ColumnNames.Add(propertyDescriptorCollection(colindex).Name)
If (colindex >= _ColumnWidths.Count) Then
_ColumnWidths.Add(_ColumnWidthDefault)
End If
_TotalWidth += _ColumnWidths(colindex)
Next
Else
_TotalWidth = 0
For colIndex As Int32 = 0 To _ColumnNames.Count Step 1
If colIndex >= _ColumnWidths.Count Then
_ColumnWidths.Add(_ColumnWidthDefault)
End If
_TotalWidth += _ColumnWidths(colIndex)
Next
End If
If (_LinkedColumnIndex >= _ColumnNames.Count) Then
_LinkedColumnIndex = 0
End If
End Sub
Private Sub InitializeValueMemberColumn()
Dim colIndex As Int32 = 0
For Each columnName As String In _ColumnNames
If String.Compare(columnName, ValueMember, True, CultureInfo.CurrentUICulture) = 0 Then
_ValueMemberColumnIndex = colIndex
Exit For
End If
colIndex += 1
Next
End Sub
End Class
Public Class Form1--------------------编程问答-------------------- 好长的代码, --------------------编程问答-------------------- 这么长,看晕的,想实现什么功能? --------------------编程问答-------------------- 就是建立一个ComboBox控件,将表的数据Employee ID绑定在控件上 --------------------编程问答-------------------- 就是实现一个表格状的combobox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim p As MultiColumnComboBox
p = New MultiColumnComboBox
Me.SuspendLayout()
'
'ComboBox1
'
p.FormattingEnabled = True
p.Location = New System.Drawing.Point(32, 40)
p.Name = "ComboBox1"
p.Size = New System.Drawing.Size(121, 20)
p.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(p)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Dim dt As New DataTable("Employees")
dt.Columns.Add("Employee ID", GetType(String))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Designation", GetType(String))
dt.Rows.Add(New String() {"D1", "Natalia", "Developer"})
dt.Rows.Add(New String() {"D2", "Jonathan", "Developer"})
dt.Rows.Add(New String() {"D3", "Jake", "Developer"})
dt.Rows.Add(New String() {"D4", "Abraham", "Developer"})
dt.Rows.Add(New String() {"T1", "Mary", "Team Lead"})
dt.Rows.Add(New String() {"PM1", "Calvin", "Project Manager"})
dt.Rows.Add(New String() {"T2", "Sarah", "Team Lead"})
dt.Rows.Add(New String() {"D12", "Monica", "Developer"})
dt.Rows.Add(New String() {"D13", "Donna", "Developer"})
p.DataSource = dt
p.DisplayMember = "Employee ID"
p.ValueMember = "Name"
End Sub
End Class
补充:.NET技术 , VB.NET