ListBox 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示用于显示项列表的 Windows 控件。
public ref class ListBox : System::Windows::Forms::ListControl
public class ListBox : System.Windows.Forms.ListControl
[System.ComponentModel.DefaultBindingProperty("SelectedValue")]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
public class ListBox : System.Windows.Forms.ListControl
[System.ComponentModel.DefaultBindingProperty("SelectedValue")]
public class ListBox : System.Windows.Forms.ListControl
type ListBox = class
inherit ListControl
[<System.ComponentModel.DefaultBindingProperty("SelectedValue")>]
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ListBox = class
inherit ListControl
[<System.ComponentModel.DefaultBindingProperty("SelectedValue")>]
type ListBox = class
inherit ListControl
Public Class ListBox
Inherits ListControl
- 继承
- 派生
- 属性
示例
下面的代码示例演示如何创建一个 ListBox 控件,该控件在列中显示多个项,并且可以在控件的列表中选择多个项。 该示例的代码使用类的方法ListBox.ObjectCollection添加 50 个项ListBoxAdd,然后使用该方法从列表中选择SetSelected三个项目。 然后,该代码通过属性显示集合中的SelectedItems值,以及ListBox.SelectedIndexCollection通过属性显示SelectedIndices的值ListBox.SelectedObjectCollection。 此示例要求代码位于 中并从中 Form调用。
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Create an instance of the ListBox.
ListBox^ listBox1 = gcnew ListBox;
// Set the size and location of the ListBox.
listBox1->Size = System::Drawing::Size( 200, 100 );
listBox1->Location = System::Drawing::Point( 10, 10 );
// Add the ListBox to the form.
this->Controls->Add( listBox1 );
// Set the ListBox to display items in multiple columns.
listBox1->MultiColumn = true;
// Set the selection mode to multiple and extended.
listBox1->SelectionMode = SelectionMode::MultiExtended;
// Shutdown the painting of the ListBox as items are added.
listBox1->BeginUpdate();
// Loop through and add 50 items to the ListBox.
for ( int x = 1; x <= 50; x++ )
{
listBox1->Items->Add( String::Format( "Item {0}", x ) );
}
listBox1->EndUpdate();
// Select three items from the ListBox.
listBox1->SetSelected( 1, true );
listBox1->SetSelected( 3, true );
listBox1->SetSelected( 5, true );
#if defined(DEBUG)
// Display the second selected item in the ListBox to the console.
System::Diagnostics::Debug::WriteLine( listBox1->SelectedItems[ 1 ] );
// Display the index of the first selected item in the ListBox.
System::Diagnostics::Debug::WriteLine( listBox1->SelectedIndices[ 0 ] );
#endif
}
private void button1_Click(object sender, System.EventArgs e)
{
// Create an instance of the ListBox.
ListBox listBox1 = new ListBox();
// Set the size and location of the ListBox.
listBox1.Size = new System.Drawing.Size(200, 100);
listBox1.Location = new System.Drawing.Point(10,10);
// Add the ListBox to the form.
this.Controls.Add(listBox1);
// Set the ListBox to display items in multiple columns.
listBox1.MultiColumn = true;
// Set the selection mode to multiple and extended.
listBox1.SelectionMode = SelectionMode.MultiExtended;
// Shutdown the painting of the ListBox as items are added.
listBox1.BeginUpdate();
// Loop through and add 50 items to the ListBox.
for (int x = 1; x <= 50; x++)
{
listBox1.Items.Add("Item " + x.ToString());
}
// Allow the ListBox to repaint and display the new items.
listBox1.EndUpdate();
// Select three items from the ListBox.
listBox1.SetSelected(1, true);
listBox1.SetSelected(3, true);
listBox1.SetSelected(5, true);
// Display the second selected item in the ListBox to the console.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString());
// Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString());
}
Private Sub button1_Click(sender As Object, e As System.EventArgs)
' Create an instance of the ListBox.
Dim listBox1 As New ListBox()
' Set the size and location of the ListBox.
listBox1.Size = New System.Drawing.Size(200, 100)
listBox1.Location = New System.Drawing.Point(10, 10)
' Add the ListBox to the form.
Me.Controls.Add(listBox1)
' Set the ListBox to display items in multiple columns.
listBox1.MultiColumn = True
' Set the selection mode to multiple and extended.
listBox1.SelectionMode = SelectionMode.MultiExtended
' Shutdown the painting of the ListBox as items are added.
listBox1.BeginUpdate()
' Loop through and add 50 items to the ListBox.
Dim x As Integer
For x = 1 To 50
listBox1.Items.Add("Item " & x.ToString())
Next x
' Allow the ListBox to repaint and display the new items.
listBox1.EndUpdate()
' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)
' Display the second selected item in the ListBox to the console.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems(1).ToString())
' Display the index of the first selected item in the ListBox.
System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices(0).ToString())
End Sub
注解
通过 ListBox 该控件,你可以向用户显示一个项列表,用户可以通过单击来选择这些项。 控件 ListBox 可以使用该 SelectionMode 属性提供单个或多个选择。 ListBox 还提供了 MultiColumn 属性,使项目以列形式显示,而不是仅仅以垂直列表形式排列。 这样,控件可以显示更可见的项,用户不再需要滚动到某个项。
通常,Windows 处理绘制要显示在其中 ListBox的项目的任务。 可以使用该 DrawMode 属性并处理 MeasureItem 和 DrawItem 事件,以便你可以替代 Windows 提供的自动绘图并自行绘制项目。 可以使用所有者绘制 ListBox 的控件显示列表中每个项的文本的可变高度项目、图像或不同颜色或字体。 该 HorizontalExtent 属性 GetItemHeight,还 GetItemRectangle 有助于绘制自己的项。
除了显示和选择功能之外,还提供了 ListBox 一项功能,使你能够有效地将项添加到 ListBox 列表的项中并查找文本。 使用 BeginUpdate 和 EndUpdate 方法,可以在每次将项添加到列表中时,无需重新绘制控件即可将大量项 ListBox 添加到列表中。 使用 FindString 和 FindStringExact 方法可以搜索列表中包含特定搜索字符串的项。
和
| 集合类 | 在 ListBox |
|---|---|
| ListBox.ObjectCollection | 包含控件中包含的 ListBox 所有项。 |
| ListBox.SelectedObjectCollection | 包含选定项的集合,该集合是控件中包含的 ListBox 项的子集。 |
| ListBox.SelectedIndexCollection | 包含所选索引的集合,该集合是该 ListBox.ObjectCollection索引的子集。 这些索引指定所选项。 |
以下三个示例显示了类支持的三个索引集合 ListBox 。
下表显示了如何在ListBox.ObjectCollection示例中ListBox存储项ListBox及其选择状态的示例。
| 索引 | 物品 | ListBox 中的选择状态 |
|---|---|---|
| 0 | object1 | 未选中 |
| 1 | object2 | 已选择 |
| 2 | object3 | 未选中 |
| 3 | object4 | 已选择 |
| 4 | object5 | 已选择 |
根据 ListBox.ObjectCollection 上表所示,此表显示显示方式 ListBox.SelectedObjectCollection 。
| 索引 | 物品 |
|---|---|
| 0 | object2 |
| 1 | object4 |
| 2 | object5 |
根据 ListBox.ObjectCollection 上表所示,此表显示显示方式 ListBox.SelectedIndexCollection 。
| 索引 | 项索引 |
|---|---|
| 0 | 1 |
| 1 | 3 |
| 2 | 4 |
类Add的方法ListBox.ObjectCollection使你能够将项添加到 .ListBox 向 .. 添加成员ListBox时,该方法Add可以接受任何对象。 将对象添加到 ListBox对象时,控件将使用对象方法中 ToString 定义的文本,除非在属性中 DisplayMember 指定了对象中的成员名称。 除了使用 Add 类的方法 ListBox.ObjectCollection 添加项之外,还可以使用 DataSource 类的属性 ListControl 添加项。
注释
如果你有一ComboBox个ListBox或CheckedListBox一个基本 Windows 窗体,并且想要修改这些控件的字符串集合在派生的 Windows 窗体中,这些控件的字符串集合必须为空。 如果字符串集合不为空,则当你派生另一个 Windows 窗体时,它们将成为只读的。
构造函数
| 名称 | 说明 |
|---|---|
| ListBox() |
初始化 ListBox 类的新实例。 |
字段
| 名称 | 说明 |
|---|---|
| DefaultItemHeight |
指定所有者绘制 ListBox的默认项高度。 |
| NoMatches |
指定在搜索过程中找不到任何匹配项。 |
属性
| 名称 | 说明 |
|---|---|
| AccessibilityObject |
AccessibleObject获取分配给控件的控件。 (继承自 Control) |
| AccessibleDefaultActionDescription |
获取或设置控件的默认操作说明,以供辅助功能客户端应用程序使用。 (继承自 Control) |
| AccessibleDescription |
获取或设置辅助功能客户端应用程序使用的控件的说明。 (继承自 Control) |
| AccessibleName |
获取或设置辅助功能客户端应用程序使用的控件的名称。 (继承自 Control) |
| AccessibleRole |
获取或设置控件的可访问角色。 (继承自 Control) |
| AllowDrop |
获取或设置一个值,该值指示控件是否可以接受用户拖动到其中的数据。 (继承自 Control) |
| AllowSelection |
获取一个值,该值指示当前是否 ListBox 允许选择列表项。 |
| Anchor |
获取或设置控件绑定到的容器的边缘,并确定控件的父级如何调整其大小。 (继承自 Control) |
| AutoScrollOffset |
获取或设置此控件滚动到的位置 ScrollControlIntoView(Control)。 (继承自 Control) |
| AutoSize |
此属性与此类无关。 (继承自 Control) |
| BackColor |
获取或设置控件的背景色。 |
| BackgroundImage |
此属性与此类无关。 |
| BackgroundImageLayout |
获取或设置枚举中ImageLayout定义的背景图像布局ListBox。 |
| BindingContext |
获取或设置 BindingContext 控件。 (继承自 Control) |
| BorderStyle |
获取或设置在周围绘制的 ListBox边框类型。 |
| Bottom |
获取控件的下边缘与其容器工作区的上边缘之间的距离(以像素为单位)。 (继承自 Control) |
| Bounds |
获取或设置控件的大小和位置,包括其相对于父控件的非client 元素(以像素为单位)。 (继承自 Control) |
| CanEnableIme |
获取一个值,该值指示属性是否可以 ImeMode 设置为活动值,以启用 IME 支持。 (继承自 Control) |
| CanFocus |
获取一个值,该值指示控件是否可以接收焦点。 (继承自 Control) |
| CanRaiseEvents |
确定是否可以在控件上引发事件。 (继承自 Control) |
| CanSelect |
获取一个值,该值指示是否可以选择控件。 (继承自 Control) |
| Capture |
获取或设置一个值,该值指示控件是否已捕获鼠标。 (继承自 Control) |
| CausesValidation |
获取或设置一个值,该值指示控件是否导致验证在收到焦点时需要验证的任何控件上执行。 (继承自 Control) |
| ClientRectangle |
获取表示控件工作区的矩形。 (继承自 Control) |
| ClientSize |
获取或设置控件工作区的高度和宽度。 (继承自 Control) |
| ColumnWidth |
获取或设置多列 ListBox的宽度。 |
| CompanyName |
获取包含控件的应用程序的公司或创建者的名称。 (继承自 Control) |
| Container |
IContainer获取包含 .Component (继承自 Component) |
| ContainsFocus |
获取一个值,该值指示控件或其子控件之一当前是否具有输入焦点。 (继承自 Control) |
| ContextMenu |
已过时.
获取或设置与控件关联的快捷菜单。 (继承自 Control) |
| ContextMenuStrip |
获取或设置 ContextMenuStrip 与此控件关联的值。 (继承自 Control) |
| Controls |
获取控件中包含的控件的集合。 (继承自 Control) |
| Created |
获取一个值,该值指示是否已创建控件。 (继承自 Control) |
| CreateParams |
获取创建控件句柄时所需的创建参数。 |
| Cursor |
获取或设置鼠标指针位于控件上时显示的光标。 (继承自 Control) |
| CustomTabOffsets |
获取项 ListBox之间的选项卡的宽度。 |
| DataBindings |
获取控件的数据绑定。 (继承自 Control) |
| DataContext |
获取或设置用于数据绑定的数据上下文。 这是一个环境属性。 (继承自 Control) |
| DataManager |
获取 CurrencyManager 与此控件关联的控件。 (继承自 ListControl) |
| DataSource |
获取或设置此 ListControl数据源。 (继承自 ListControl) |
| DefaultCursor |
获取或设置控件的默认游标。 (继承自 Control) |
| DefaultImeMode |
获取控件支持的默认输入法编辑器 (IME) 模式。 (继承自 Control) |
| DefaultMargin |
获取默认情况下在控件之间指定的空间(以像素为单位)。 (继承自 Control) |
| DefaultMaximumSize |
获取指定为控件的默认最大大小的长度和高度(以像素为单位)。 (继承自 Control) |
| DefaultMinimumSize |
获取指定为控件的默认最小大小的长度和高度(以像素为单位)。 (继承自 Control) |
| DefaultPadding |
获取控件内容的默认内部间距(以像素为单位)。 (继承自 Control) |
| DefaultSize |
获取控件的默认大小。 |
| DesignMode |
获取一个值,该值指示当前是否 Component 处于设计模式。 (继承自 Component) |
| DeviceDpi |
获取当前显示控件的显示设备的 DPI 值。 (继承自 Control) |
| DisplayMember |
获取或设置要为此 ListControl显示的属性。 (继承自 ListControl) |
| DisplayRectangle |
获取表示控件的显示区域的矩形。 (继承自 Control) |
| Disposing |
获取一个值,该值指示基 Control 类是否正在处理。 (继承自 Control) |
| Dock |
获取或设置哪些控件边框停靠到其父控件,并确定控件如何调整其父级的大小。 (继承自 Control) |
| DoubleBuffered |
获取或设置一个值,该值指示此控件是否应使用辅助缓冲区重新绘制其表面以减少或防止闪烁。 (继承自 Control) |
| DrawMode |
获取或设置控件的绘图模式。 |
| Enabled |
获取或设置一个值,该值指示控件是否可以响应用户交互。 (继承自 Control) |
| Events |
获取附加到此 Component对象的事件处理程序的列表。 (继承自 Component) |
| Focused |
获取一个值,该值指示控件是否具有输入焦点。 (继承自 Control) |
| Font |
获取或设置控件显示的文本的字体。 |
| Font |
获取或设置控件显示的文本的字体。 (继承自 Control) |
| FontHeight |
获取或设置控件字体的高度。 (继承自 Control) |
| ForeColor |
获取或设置控件的前景色。 |
| FormatInfo |
获取或设置 IFormatProvider 提供自定义格式设置行为的设置。 (继承自 ListControl) |
| FormatString |
获取或设置指示如何显示值的格式说明符字符。 (继承自 ListControl) |
| FormattingEnabled |
获取或设置一个值,该值指示是否将格式应用于DisplayMember该属性。ListControl (继承自 ListControl) |
| Handle |
获取控件绑定到的窗口句柄。 (继承自 Control) |
| HasChildren |
获取一个值,该值指示控件是否包含一个或多个子控件。 (继承自 Control) |
| Height |
获取或设置控件的高度。 (继承自 Control) |
| HorizontalExtent |
获取或设置可滚动的水平滚动条 ListBox 的宽度。 |
| HorizontalScrollbar |
获取或设置一个值,该值指示是否在控件中显示水平滚动条。 |
| ImeMode |
获取或设置控件的输入法编辑器 (IME) 模式。 (继承自 Control) |
| ImeModeBase |
获取或设置控件的 IME 模式。 (继承自 Control) |
| IntegralHeight |
获取或设置一个值,该值指示控件是否应调整大小以避免显示部分项。 |
| InvokeRequired |
获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用调用方法,因为调用方与创建控件的线程不同。 (继承自 Control) |
| IsAccessible |
获取或设置一个值,该值指示控件是否对辅助功能应用程序可见。 (继承自 Control) |
| IsAncestorSiteInDesignMode |
指示此控件的上级位置之一是否位于 DesignMode 中。 此属性为只读。 (继承自 Control) |
| IsDisposed |
获取一个值,该值指示控件是否已释放。 (继承自 Control) |
| IsHandleCreated |
获取一个值,该值指示控件是否具有与之关联的句柄。 (继承自 Control) |
| IsMirrored |
获取一个值,该值指示控件是否镜像。 (继承自 Control) |
| ItemHeight |
获取或设置项在 . 中的 ListBox高度。 |
| Items |
获取 . 的 ListBox项 |
| LayoutEngine |
获取控件布局引擎的缓存实例。 (继承自 Control) |
| Left |
获取或设置控件左边缘与其容器工作区的左边缘之间的距离(以像素为单位)。 (继承自 Control) |
| Location |
获取或设置控件左上角相对于其容器左上角的坐标。 (继承自 Control) |
| Margin |
获取或设置控件之间的间距。 (继承自 Control) |
| MaximumSize |
获取或设置可指定上限 GetPreferredSize(Size) 的大小。 (继承自 Control) |
| MinimumSize |
获取或设置可以指定的下限 GetPreferredSize(Size) 的大小。 (继承自 Control) |
| MultiColumn |
获取或设置一个值,该值指示是否 ListBox 支持多个列。 |
| Name |
获取或设置控件的名称。 (继承自 Control) |
| Padding |
此属性与此类无关。 |
| Parent |
获取或设置控件的父容器。 (继承自 Control) |
| PreferredHeight |
获取 .. 中 ListBox所有项的组合高度。 |
| PreferredSize |
获取控件可以容纳到的矩形区域的大小。 (继承自 Control) |
| ProductName |
获取包含控件的程序集的产品名称。 (继承自 Control) |
| ProductVersion |
获取包含控件的程序集的版本。 (继承自 Control) |
| RecreatingHandle |
获取一个值,该值指示控件当前是否正在重新创建其句柄。 (继承自 Control) |
| Region |
获取或设置与控件关联的窗口区域。 (继承自 Control) |
| RenderRightToLeft |
已过时.
已过时.
此属性现已过时。 (继承自 Control) |
| ResizeRedraw |
获取或设置一个值,该值指示控件在调整大小时是否重新绘制自身。 (继承自 Control) |
| Right |
获取控件右边缘与其容器工作区的左边缘之间的距离(以像素为单位)。 (继承自 Control) |
| RightToLeft |
获取或设置一个值,该值指示控件显示的文本是否从右到左显示。 |
| RightToLeft |
获取或设置一个值,该值指示控件的元素是否对齐以支持使用从右到左字体的区域设置。 (继承自 Control) |
| ScaleChildren |
获取一个值,该值确定子控件的缩放。 (继承自 Control) |
| ScrollAlwaysVisible |
获取或设置一个值,该值指示是否随时显示垂直滚动条。 |
| SelectedIndex |
获取或设置当前选定项的 ListBox从零开始的索引。 |
| SelectedIndices |
获取一个集合,该集合包含当前所有选定项的 ListBox从零开始的索引。 |
| SelectedItem |
获取或设置当前选定的项。ListBox |
| SelectedItems |
获取包含当前选定项的 ListBox集合。 |
| SelectedValue |
获取或设置由该属性指定的 ValueMember 成员属性的值。 (继承自 ListControl) |
| SelectionMode |
获取或设置在其中选择 ListBox项的方法。 |
| ShowFocusCues |
获取一个值,该值指示控件是否应显示焦点矩形。 (继承自 Control) |
| ShowKeyboardCues |
获取一个值,该值指示用户界面是否处于适当的状态以显示或隐藏键盘加速器。 (继承自 Control) |
| Site |
获取或设置控件的站点。 (继承自 Control) |
| Size |
获取或设置控件的高度和宽度。 (继承自 Control) |
| Sorted |
获取或设置一个值,该值指示项 ListBox 是否按字母顺序排序。 |
| TabIndex |
获取或设置控件在其容器中的 Tab 键顺序。 (继承自 Control) |
| TabStop |
获取或设置一个值,该值指示用户是否可以使用 TAB 键向此控件提供焦点。 (继承自 Control) |
| Tag |
获取或设置包含有关控件的数据的对象。 (继承自 Control) |
| Text |
获取或搜索当前选定项的文本 ListBox。 |
| Top |
获取或设置控件上边缘与其容器工作区上边缘之间的距离(以像素为单位)。 (继承自 Control) |
| TopIndex |
获取或设置第 ListBox一个可见项的索引。 |
| TopLevelControl |
获取其他 Windows 窗体控件未父控件的父控件。 通常,这是控件包含在的最外层 Form 。 (继承自 Control) |
| UseCustomTabOffsets |
获取或设置一个值,该值指示在使用整数数组绘制其字符串CustomTabOffsets时,它是否ListBox识别并展开制表符。 |
| UseTabStops |
获取或设置一个值,该值指示在绘制其字符串时是否可以 ListBox 识别和展开制表符。 |
| UseWaitCursor |
获取或设置一个值,该值指示是否对当前控件和所有子控件使用等待游标。 (继承自 Control) |
| ValueMember |
获取或设置要用作项的实际 ListControl值的属性的路径。 (继承自 ListControl) |
| Visible |
获取或设置一个值,该值指示是否显示控件及其所有子控件。 (继承自 Control) |
| Width |
获取或设置控件的宽度。 (继承自 Control) |
| WindowTarget |
此属性与此类无关。 (继承自 Control) |
方法
活动
显式接口实现
| 名称 | 说明 |
|---|---|
| IDropTarget.OnDragDrop(DragEventArgs) |
引发 DragDrop 事件。 (继承自 Control) |
| IDropTarget.OnDragEnter(DragEventArgs) |
引发 DragEnter 事件。 (继承自 Control) |
| IDropTarget.OnDragLeave(EventArgs) |
引发 DragLeave 事件。 (继承自 Control) |
| IDropTarget.OnDragOver(DragEventArgs) |
引发 DragOver 事件。 (继承自 Control) |