Share via


ListBox 类

表示用于显示项列表的 Windows 控件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class ListBox
    Inherits ListControl
用法
Dim instance As ListBox
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class ListBox : ListControl
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class ListBox : public ListControl
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class ListBox extends ListControl
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class ListBox extends ListControl

备注

ListBox 控件使您得以向用户显示一列项,用户可通过单击选择这些项。ListBox 控件可使用 SelectionMode 属性提供单项选择或多重选择。ListBox 还提供 MultiColumn 属性,以启用按多列显示项而不是项的垂直列表。这样,控件便可以显示更多可见项,用户不再需要滚动到某项进行查看。

通常,Windows 处理绘制在 ListBox 中显示的项的任务。您可以使用 DrawMode 属性并处理 MeasureItemDrawItem 事件,以重写 Windows 所提供的自动绘制,自己对项进行绘制。可以使用所有者描述的 ListBox 控件显示高度可变的项、图像或者为列表中每个项的文本显示不同的颜色或字体。HorizontalExtent 属性、GetItemHeightGetItemRectangle 也可以帮助您绘制自己的项。

除了显示和选择功能外,ListBox 还提供一些功能,使您得以有效地将项添加到 ListBox 中以及在列表的项内查找文本。BeginUpdateEndUpdate 方法使您得以将大量项添加到 ListBox 中,而不必每次将一个项添加到列表中时都重新绘制该控件。FindStringFindStringExact 方法使您得以在列表中搜索包含特定搜索字符串的项。

ItemsSelectedItemsSelectedIndices 属性提供对 ListBox 所使用的三个集合的访问。下表概述 ListBox 使用的三个集合及其在控件内的用途。

集合类

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

ListBox.ObjectCollection 类的 Add 方法使您得以将项添加到 ListBox 中。当向 ListBox 添加成员时,Add 方法可接受任何对象。当向 ListBox 中添加对象时,该控件使用在该对象的 ToString 方法中定义的文本,除非在 DisplayMember 属性中指定了该对象内的成员名。除了使用 ListBox.ObjectCollection 类的 Add 方法添加项外,还可以使用 ListControl 类的 DataSource 属性添加项。

提示

如果在基 Windows 窗体中有一个 ListBoxComboBoxCheckedListBox,并且您要在派生 Windows 窗体中修改这些控件的字符串集合,则基 Windows 窗体中的这些控件的字符串集合必须是空的。如果字符串集合不为空,则当派生其他 Windows 窗体时,它们将成为只读字符串集合。

示例

下面的代码示例演示如何创建一个 ListBox 控件,该控件可在列中显示多个项,并且可以在控件列表中选定多个项。该示例的代码使用 ListBox.ObjectCollection 类的 Add 方法向 ListBox 添加 50 个项,然后使用 SetSelected 方法从列表中选择三项。然后,代码显示 ListBox.SelectedObjectCollection 集合中的值(通过 SelectedItems 属性)和 ListBox.SelectedIndexCollection 中的值(通过 SelectedIndices 属性)。此示例要求代码位于 Form 中并从中调用。

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
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());             
}
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 );
   
   // 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 ] );
}
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.set_Size(new System.Drawing.Size(200,100));
    listBox1.set_Location(new System.Drawing.Point(10,10));

    // Add the ListBox to the form.
    this.get_Controls().Add(listBox1);

    // Set the ListBox to display items in multiple columns.
    listBox1.set_MultiColumn(true);

    // Set the selection mode to multiple and extended.
    listBox1.set_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.get_Items().Add(("Item" + (new Integer(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.get_SelectedItems().get_Item(1).ToString());

    // Display the index of the first selected item in the ListBox.
    System.Diagnostics.Debug.WriteLine((new Integer
        (listBox1.get_SelectedIndices().get_Item(0))).ToString());
} //button1_Click
private function button1_Click(sender : Object, e : System.EventArgs)
{
   // Create an instance of the ListBox.
   var listBox1 : ListBox = 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 (var x : int = 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());             
}

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ListControl
          System.Windows.Forms.ListBox
             System.Windows.Forms.CheckedListBox

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、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

请参见

参考

ListBox 成员
System.Windows.Forms 命名空间