目录

ComboBox

ComboBox控件用于显示各种项目的下拉列表。 它是用户输入项目的文本框和用户选择项目的下拉列表的组合。

让我们通过从工具箱中拖动一个ComboBox控件并将其放在窗体上来创建一个组合框。

VB.Net ComboBox控件

您可以从属性窗口或运行时填充列表框项。 要将项添加到ComboBox,请选择ComboBox控件并转到属性窗口以获取此控件的属性。 单击Items属性旁边的省略号(...)按钮。 这将打开“字符串集合编辑器”对话框,您可以在其中输入一行中的值。

ComboBox控件的属性

以下是ComboBox控件的一些常用属性 -

Sr.No. 财产和描述
1

AllowSelection

获取一个值,该值指示列表是否允许选择列表项。

2

AutoCompleteCustomSource

获取或设置AutoCompleteSource属性设置为CustomSource时要使用的自定义System.Collections .Specialized.StringCollection。

3

AutoCompleteMode

获取或设置一个选项,该选项控制ComboBox的自动完成工作方式。

4

AutoCompleteSource

获取或设置一个值,该值指定用于自动完成的完整字符串的来源。

5

DataBindings

获取控件的数据绑定。

6

DataManager

获取与此控件关联的CurrencyManager。

7

DataSource

获取或设置此ComboBox的数据源。

8

DropDownHeight

获取或设置ComboBox下拉部分的高度(以像素为单位)。

9

DropDownStyle

获取或设置一个指定组合框样式的值。

10

DropDownWidth

获取或设置组合框的下拉部分的宽度。

11

DroppedDown

获取或设置一个值,该值指示组合框是否正在显示其下拉部分。

12

FlatStyle

获取或设置ComboBox的外观。

13

ItemHeight

获取或设置组合框中项的高度。

14

Items

获取一个对象,该对象表示此ComboBox中包含的项的集合。

15

MaxDropDownItems

获取或设置要在组合框的下拉部分中显示的最大项目数。

16

MaxLength

获取或设置用户可以在组合框的可编辑区域中输入的最大字符数。

17

SelectedIndex

获取或设置指定当前所选项的索引。

18

SelectedItem

获取或设置ComboBox中当前选定的项目。

19

SelectedText

获取或设置在ComboBox的可编辑部分中选择的文本。

20

SelectedValue

获取或设置ValueMember属性指定的成员属性的值。

21

SelectionLength

获取或设置在组合框的可编辑部分中选择的字符数。

22

SelectionStart

获取或设置组合框中所选文本的起始索引。

23

Sorted

获取或设置一个值,该值指示组合框中的项是否已排序。

24

Text

获取或设置与此控件关联的文本。

ComboBox控件的方法

以下是ComboBox控件的一些常用方法 -

Sr.No. 方法名称和描述
1

BeginUpdate

在调用EndUpdate方法之前阻止控件绘制,而将项目一次添加到组合框中。

2

EndUpdate

在BeginUpdate方法关闭后,继续绘制组合框。

3

FindString

查找组合框中以指定为参数的字符串开头的第一个项目。

4

FindStringExact

查找组合框中与指定字符串完全匹配的第一项。

5

SelectAll

选择组合框的可编辑区域中的所有文本。

ComboBox控件的事件

以下是ComboBox控件的一些常用事件 -

Sr.No. 活动和描述
1

DropDown

显示组合框的下拉部分时发生。

2

DropDownClosed

当组合框的下拉部分不再可见时发生。

3

DropDownStyleChanged

在ComboBox的DropDownStyle属性发生更改时发生。

4

SelectedIndexChanged

在ComboBox控件的SelectedIndex属性更改时发生。

5

SelectionChangeCommitted

所选项目发生更改并且组合框中显示更改时发生。

例子 (Example)

在此示例中,让我们填充包含各种项目的组合框,在组合框中获取所选项目并在列表框中显示它们并对项目进行排序。

拖放组合框以存储项目,列表框显示所选项目,四个按钮控件添加到包含所选项目的列表框,填充组合框,排序项目和清除组合框列表, 分别。

添加将显示所选项目的标签控件。

结果表格

在代码编辑器窗口中添加以下代码 -

Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.  
      Me.Text = "tutorialspont.com"
   End Sub
   'sends the selected items to the list box
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      If ComboBox1.SelectedIndex > -1 Then
          Dim sindex As Integer
          sindex = ComboBox1.SelectedIndex
          Dim sitem As Object
          sitem = ComboBox1.SelectedItem
          ListBox1.Items.Add(sitem)
      End If
   End Sub
   'populates the list
   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
      ComboBox1.Items.Clear()
      ComboBox1.Items.Add("Safety")
      ComboBox1.Items.Add("Security")
      ComboBox1.Items.Add("Governance")
      ComboBox1.Items.Add("Good Music")
      ComboBox1.Items.Add("Good Movies")
      ComboBox1.Items.Add("Good Books")
      ComboBox1.Items.Add("Education")
      ComboBox1.Items.Add("Roads")
      ComboBox1.Items.Add("Health")
      ComboBox1.Items.Add("Food for all")
      ComboBox1.Items.Add("Shelter for all")
      ComboBox1.Items.Add("Industrialisation")
      ComboBox1.Items.Add("Peace")
      ComboBox1.Items.Add("Liberty")
      ComboBox1.Items.Add("Freedom of Speech")
      ComboBox1.Text = "Select from..."
   End Sub
   'sorting the list
   Private Sub Button3_Click(sender As Object, e As EventArgs)
      ComboBox1.Sorted = True
   End Sub
   'clears the list
   Private Sub Button4_Click(sender As Object, e As EventArgs)
      ComboBox1.Items.Clear()
   End Sub
   'displaying the selected item on the label
   Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
     Handles ListBox1.SelectedIndexChanged
      Label1.Text = ComboBox1.SelectedItem.ToString()
   End Sub
End Class

当使用Microsoft Visual Studio工具栏上的“ Start按钮执行并运行上述代码时,它将显示以下窗口 -

结果表格

单击各种按钮以检查每个按钮执行的操作 -

结果表格
↑回到顶部↑
WIKI教程 @2018