在VB2005中combobox的用法
展开全部
表示 Windows 组合框控件。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
语法
Visual Basic(声明)
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class ComboBox
Inherits ListControl
Visual Basic(用法)
Dim instance As ComboBox
备注
ComboBox 显示与一个 ListBox 组合的文本框编辑字段,使用户可以从列表中选择项,也可以输入新文本。ComboBox 的默认行为是显示一个编辑字段,该字段具有一个隐藏的下拉列表。DropDownStyle 属性确定要显示的组合框的样式。可以输入一个值,该值提供以下功能:简单的下拉列表(始终显示列表)、下拉列表框(文本部分不可编辑,并且必须选择一个箭头才能查看下拉列表框)或默认下拉列表框(文本部分可编辑,并且用户必须按箭头键才能查看列表)。若要显示用户不能编辑的列表,请使用 ListBox 控件。
若要在运行时向列表添加对象,请用 AddRange 方法分配一个对象引用数组。然后,列表显示每个对象的默认字符串值。可以用 Add 方法添加单个对象。
除了显示和选择功能外,ComboBox 还提供一些功能,使您得以有效地将项添加到 ComboBox 中以及在列表的项内查找文本。使用 BeginUpdate 和 EndUpdate 方法,可以将大量项添加到 ComboBox 中,而无需在每次将一个项添加到列表中时都重新绘制该控件。FindString 和 FindStringExact 方法使您得以在列表中搜索包含特定搜索字符串的项。
可以使用这些属性管理列表中当前选定的项,使用 Text 属性指定编辑字段中显示的字符串,使用 SelectedIndex 属性获取或设置当前项,以及使用 SelectedItem 属性获取或设置对对象的引用。
注意
如果在基 Windows 窗体页中有一个 ListBox、ComboBox 或 CheckedListBox,并且要在派生窗体中修改这些控件的字符串集合,则基窗体中的这些控件的字符串集合必须是空的。如果字符串集合不为空,则当派生其他窗体时,它们将成为只读字符串集合。
示例
下面的代码示例是一个完整的应用程序,演示可如何使用 Add 方法向 ComboBox 添加项,如何使用 FindString 方法在 ComboBox 中查找项,以及如何使用 BeginUpdate 和 EndUpdate 方法以高效的方式向 ComboBox 添加大量项。
Visual Basic 复制代码
Imports System
Imports System.Windows.Forms
Namespace ComboBoxSampleNamespace
Public Class ComboBoxSample
Inherits System.Windows.Forms.Form
Private addButton As System.Windows.Forms.Button
Private textBox2 As System.Windows.Forms.TextBox
Private addGrandButton As System.Windows.Forms.Button
Private comboBox1 As System.Windows.Forms.ComboBox
Private showSelectedButton As System.Windows.Forms.Button
Private textBox1 As System.Windows.Forms.TextBox
Private findButton As System.Windows.Forms.Button
Private label1 As System.Windows.Forms.Label
Public Sub New()
MyBase.New()
Me.InitializeComponent()
End Sub
<System.STAThreadAttribute()> Public Shared Sub Main()
System.Windows.Forms.Application.Run(New ComboBoxSample())
End Sub
Private Sub InitializeComponent()
Me.addButton = New System.Windows.Forms.Button()
Me.textBox2 = New System.Windows.Forms.TextBox()
Me.addGrandButton = New System.Windows.Forms.Button()
Me.comboBox1 = New System.Windows.Forms.ComboBox()
Me.showSelectedButton = New System.Windows.Forms.Button()
Me.textBox1 = New System.Windows.Forms.TextBox()
Me.findButton = New System.Windows.Forms.Button()
Me.label1 = New System.Windows.Forms.Label()
Me.addButton.Location = New System.Drawing.Point(248, 32)
Me.addButton.Size = New System.Drawing.Size(40, 24)
Me.addButton.TabIndex = 1
Me.addButton.Text = "Add"
AddHandler Me.addButton.Click, AddressOf Me.addButton_Click
Me.textBox2.Location = New System.Drawing.Point(8, 64)
Me.textBox2.Size = New System.Drawing.Size(232, 20)
Me.textBox2.TabIndex = 6
Me.textBox2.Text = ""
Me.addGrandButton.Location = New System.Drawing.Point(8, 96)
Me.addGrandButton.Size = New System.Drawing.Size(280, 23)
Me.addGrandButton.TabIndex = 2
Me.addGrandButton.Text = "Add 1,000 Items"
AddHandler Me.addGrandButton.Click, AddressOf Me.addGrandButton_Click
Me.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.comboBox1.DropDownWidth = 280
Me.comboBox1.Items.AddRange(New Object() {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"})
Me.comboBox1.Location = New System.Drawing.Point(8, 248)
Me.comboBox1.Size = New System.Drawing.Size(280, 21)
Me.comboBox1.TabIndex = 7
Me.showSelectedButton.Location = New System.Drawing.Point(8, 128)
Me.showSelectedButton.Size = New System.Drawing.Size(280, 24)
Me.showSelectedButton.TabIndex = 4
Me.showSelectedButton.Text = "What Item is Selected?"
AddHandler Me.showSelectedButton.Click, AddressOf Me.showSelectedButton_Click
Me.textBox1.Location = New System.Drawing.Point(8, 32)
Me.textBox1.Size = New System.Drawing.Size(232, 20)
Me.textBox1.TabIndex = 5
Me.textBox1.Text = ""
Me.findButton.Location = New System.Drawing.Point(248, 64)
Me.findButton.Size = New System.Drawing.Size(40, 24)
Me.findButton.TabIndex = 3
Me.findButton.Text = "Find"
AddHandler Me.findButton.Click, AddressOf Me.findButton_Click
Me.label1.Location = New System.Drawing.Point(8, 224)
Me.label1.Size = New System.Drawing.Size(144, 23)
Me.label1.TabIndex = 0
Me.label1.Text = "Test ComboBox"
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.comboBox1, Me.textBox2, Me.textBox1, Me.showSelectedButton, Me.findButton, Me.addGrandButton, Me.addButton, Me.label1})
Me.Text = "ComboBox Sample"
End Sub
Private Sub addButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
comboBox1.Items.Add(textBox1.Text)
End Sub
Private Sub findButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
Private Sub addGrandButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
comboBox1.BeginUpdate()
Dim I As Integer
For I = 0 To 1000
comboBox1.Items.Add("Item 1" + i.ToString())
Next
comboBox1.EndUpdate()
End Sub
Private Sub showSelectedButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim selectedIndex As Integer
selectedIndex = comboBox1.SelectedIndex
Dim selectedItem As Object
selectedItem = comboBox1.SelectedItem
MessageBox.Show("Selected Item Text: " & selectedItem.ToString() & Microsoft.VisualBasic.Constants.vbCrLf & _
"Index: " & selectedIndex.ToString())
End Sub
End Class
End Namespace
继承层次结构
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ListControl
System.Windows.Forms.ComboBox
Microsoft.CLRAdmin.DataGridComboBox
System.Windows.Forms.DataGridViewComboBoxEditingControl
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
ComboBox 成员
System.Windows.Forms 命名空间
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
语法
Visual Basic(声明)
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class ComboBox
Inherits ListControl
Visual Basic(用法)
Dim instance As ComboBox
备注
ComboBox 显示与一个 ListBox 组合的文本框编辑字段,使用户可以从列表中选择项,也可以输入新文本。ComboBox 的默认行为是显示一个编辑字段,该字段具有一个隐藏的下拉列表。DropDownStyle 属性确定要显示的组合框的样式。可以输入一个值,该值提供以下功能:简单的下拉列表(始终显示列表)、下拉列表框(文本部分不可编辑,并且必须选择一个箭头才能查看下拉列表框)或默认下拉列表框(文本部分可编辑,并且用户必须按箭头键才能查看列表)。若要显示用户不能编辑的列表,请使用 ListBox 控件。
若要在运行时向列表添加对象,请用 AddRange 方法分配一个对象引用数组。然后,列表显示每个对象的默认字符串值。可以用 Add 方法添加单个对象。
除了显示和选择功能外,ComboBox 还提供一些功能,使您得以有效地将项添加到 ComboBox 中以及在列表的项内查找文本。使用 BeginUpdate 和 EndUpdate 方法,可以将大量项添加到 ComboBox 中,而无需在每次将一个项添加到列表中时都重新绘制该控件。FindString 和 FindStringExact 方法使您得以在列表中搜索包含特定搜索字符串的项。
可以使用这些属性管理列表中当前选定的项,使用 Text 属性指定编辑字段中显示的字符串,使用 SelectedIndex 属性获取或设置当前项,以及使用 SelectedItem 属性获取或设置对对象的引用。
注意
如果在基 Windows 窗体页中有一个 ListBox、ComboBox 或 CheckedListBox,并且要在派生窗体中修改这些控件的字符串集合,则基窗体中的这些控件的字符串集合必须是空的。如果字符串集合不为空,则当派生其他窗体时,它们将成为只读字符串集合。
示例
下面的代码示例是一个完整的应用程序,演示可如何使用 Add 方法向 ComboBox 添加项,如何使用 FindString 方法在 ComboBox 中查找项,以及如何使用 BeginUpdate 和 EndUpdate 方法以高效的方式向 ComboBox 添加大量项。
Visual Basic 复制代码
Imports System
Imports System.Windows.Forms
Namespace ComboBoxSampleNamespace
Public Class ComboBoxSample
Inherits System.Windows.Forms.Form
Private addButton As System.Windows.Forms.Button
Private textBox2 As System.Windows.Forms.TextBox
Private addGrandButton As System.Windows.Forms.Button
Private comboBox1 As System.Windows.Forms.ComboBox
Private showSelectedButton As System.Windows.Forms.Button
Private textBox1 As System.Windows.Forms.TextBox
Private findButton As System.Windows.Forms.Button
Private label1 As System.Windows.Forms.Label
Public Sub New()
MyBase.New()
Me.InitializeComponent()
End Sub
<System.STAThreadAttribute()> Public Shared Sub Main()
System.Windows.Forms.Application.Run(New ComboBoxSample())
End Sub
Private Sub InitializeComponent()
Me.addButton = New System.Windows.Forms.Button()
Me.textBox2 = New System.Windows.Forms.TextBox()
Me.addGrandButton = New System.Windows.Forms.Button()
Me.comboBox1 = New System.Windows.Forms.ComboBox()
Me.showSelectedButton = New System.Windows.Forms.Button()
Me.textBox1 = New System.Windows.Forms.TextBox()
Me.findButton = New System.Windows.Forms.Button()
Me.label1 = New System.Windows.Forms.Label()
Me.addButton.Location = New System.Drawing.Point(248, 32)
Me.addButton.Size = New System.Drawing.Size(40, 24)
Me.addButton.TabIndex = 1
Me.addButton.Text = "Add"
AddHandler Me.addButton.Click, AddressOf Me.addButton_Click
Me.textBox2.Location = New System.Drawing.Point(8, 64)
Me.textBox2.Size = New System.Drawing.Size(232, 20)
Me.textBox2.TabIndex = 6
Me.textBox2.Text = ""
Me.addGrandButton.Location = New System.Drawing.Point(8, 96)
Me.addGrandButton.Size = New System.Drawing.Size(280, 23)
Me.addGrandButton.TabIndex = 2
Me.addGrandButton.Text = "Add 1,000 Items"
AddHandler Me.addGrandButton.Click, AddressOf Me.addGrandButton_Click
Me.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.comboBox1.DropDownWidth = 280
Me.comboBox1.Items.AddRange(New Object() {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"})
Me.comboBox1.Location = New System.Drawing.Point(8, 248)
Me.comboBox1.Size = New System.Drawing.Size(280, 21)
Me.comboBox1.TabIndex = 7
Me.showSelectedButton.Location = New System.Drawing.Point(8, 128)
Me.showSelectedButton.Size = New System.Drawing.Size(280, 24)
Me.showSelectedButton.TabIndex = 4
Me.showSelectedButton.Text = "What Item is Selected?"
AddHandler Me.showSelectedButton.Click, AddressOf Me.showSelectedButton_Click
Me.textBox1.Location = New System.Drawing.Point(8, 32)
Me.textBox1.Size = New System.Drawing.Size(232, 20)
Me.textBox1.TabIndex = 5
Me.textBox1.Text = ""
Me.findButton.Location = New System.Drawing.Point(248, 64)
Me.findButton.Size = New System.Drawing.Size(40, 24)
Me.findButton.TabIndex = 3
Me.findButton.Text = "Find"
AddHandler Me.findButton.Click, AddressOf Me.findButton_Click
Me.label1.Location = New System.Drawing.Point(8, 224)
Me.label1.Size = New System.Drawing.Size(144, 23)
Me.label1.TabIndex = 0
Me.label1.Text = "Test ComboBox"
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.comboBox1, Me.textBox2, Me.textBox1, Me.showSelectedButton, Me.findButton, Me.addGrandButton, Me.addButton, Me.label1})
Me.Text = "ComboBox Sample"
End Sub
Private Sub addButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
comboBox1.Items.Add(textBox1.Text)
End Sub
Private Sub findButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
Private Sub addGrandButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
comboBox1.BeginUpdate()
Dim I As Integer
For I = 0 To 1000
comboBox1.Items.Add("Item 1" + i.ToString())
Next
comboBox1.EndUpdate()
End Sub
Private Sub showSelectedButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim selectedIndex As Integer
selectedIndex = comboBox1.SelectedIndex
Dim selectedItem As Object
selectedItem = comboBox1.SelectedItem
MessageBox.Show("Selected Item Text: " & selectedItem.ToString() & Microsoft.VisualBasic.Constants.vbCrLf & _
"Index: " & selectedIndex.ToString())
End Sub
End Class
End Namespace
继承层次结构
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ListControl
System.Windows.Forms.ComboBox
Microsoft.CLRAdmin.DataGridComboBox
System.Windows.Forms.DataGridViewComboBoxEditingControl
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
ComboBox 成员
System.Windows.Forms 命名空间
参考资料: MSDN
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询