ListBox 控件概述(Windows 窗体)ListBox Control Overview (Windows Forms)

Windows 窗体ListBox控件显示用户可以从中选择一个或多个项列表。A Windows Forms ListBox control displays a list from which the user can select one or more items. 如果总项数超出可显示的数字,滚动条自动添加到ListBox控件。If the total number of items exceeds the number that can be displayed, a scroll bar is automatically added to the ListBox control. MultiColumn属性设置为true、 列表框中显示多个列中的项和水平滚动条的显示。When the MultiColumn property is set to true, the list box displays items in multiple columns and a horizontal scroll bar appears. MultiColumn属性设置为false、 列表框中显示的单个列中的项和垂直滚动条的显示。When the MultiColumn property is set to false, the list box displays items in a single column and a vertical scroll bar appears. ScrollAlwaysVisible设置为true,该滚动条显示而不考虑项的数目。When ScrollAlwaysVisible is set to true, the scroll bar appears regardless of the number of items. SelectionMode属性确定一次可以选择多少个列表项。The SelectionMode property determines how many list items can be selected at a time.

若要更改 ListBox 控件的方式Ways to Change the ListBox Control

SelectedIndex属性返回对应的整数值的列表框中的第一个所选项目。The SelectedIndex property returns an integer value that corresponds to the first selected item in the list box. 你可以以编程方式更改选定的项,通过更改SelectedIndex代码中的值; 将 Windows 窗体上突出显示列表中的相应项。You can programmatically change the selected item by changing the SelectedIndex value in code; the corresponding item in the list will appear highlighted on the Windows Form. 如果未不选择任何项,SelectedIndex值为-1。If no item is selected, the SelectedIndex value is -1. 如果选择列表中的第一项,则SelectedIndex值为 0。If the first item in the list is selected, the SelectedIndex value is 0. 当选择了多个项时,SelectedIndex值反映在列表中最先出现的选定的项。When multiple items are selected, the SelectedIndex value reflects the selected item that appears first in the list. SelectedItem属性是类似于SelectedIndex,但返回项本身,而通常一个字符串值。The SelectedItem property is similar to SelectedIndex, but returns the item itself, usually a string value. Count属性反映了在列表中,项的数目和的值Count属性始终为一个超过最大可能SelectedIndex值,因为SelectedIndex是从零开始。The Count property reflects the number of items in the list, and the value of the Count property is always one more than the largest possible SelectedIndex value because SelectedIndex is zero-based.

若要添加或删除中的项ListBox控制,请使用AddInsertClearRemove方法。To add or delete items in a ListBox control, use the Add, Insert, Clear or Remove method. 或者,您可以向列表添加项使用Items在设计时属性。Alternatively, you can add items to the list by using the Items property at design time.

请参阅See also