About ListBox
ListBox is a list control in WinForm that provides a list of items (a set of data items) from which users can select one or more entries. When the list items are excessive, the ListBox automatically adds a scrollbar, allowing users to scroll through all options. The ListBox can have its items predefined, or it can be bound to other controls or databases, updating entries automatically and displaying the data one by one.
Common Properties of ListBox
* List index value refers to the sequence number of the items in the list, starting from 0, such as 0, 1, 2, 3... When performing operations like add, delete, or insert on the list, the order of the items changes, and the index will also change.
* Selected item index refers to the number of the selected items, also starting from 0. When performing actions like adding or deleting on the selected items, the number changes, and so does the index. For instance, if my name is 痴者工良 and I am arranged by the pinyin of my name, I have the student number 66 in class, but among the 10 students with the surname 痴, I am number 3.
The difference between the two is as follows (items with colored background are selected):
Common Properties | Description |
Items | Refers to the collection of all items in the list, which is an array collection. This property can be used to add, remove or retrieve the contents of the list. |
MultiColumn | Used to set or get a value (bool value) indicating whether multiple columns are allowed to display, where true represents multiple columns and false represents a single column, with the default being false. |
ColumnWidth | Used to get or set the current width of the column in the list. |
SelectionMode |
Sets the method of selection for the list items. SelectionMode.None indicates that no selection is allowed. SelectionMode.One indicates that only one item can be selected by the user. SelectionMode.MultiExtended indicates that multiple items can be selected, but the selected items must be contiguous (adjacent). SelectionMode.MultiSimple indicates that multiple items can be selected, allowing the selection of any number of entries. As shown in the figure below:
|
SelectedIndex |
* Gets the index of the selected item. If no item is selected, the return value is -1. In single selection, the property value is the index of the selected item. In multiple selections, it represents the index of the first selected item, and you can also use SelectedIndex[i] to get the indices of other selected items or to make an item selected. i is the sequence number of the selected item starting from 0. |
SelectedIndices | Used to obtain the collection of indices of the selected items starting from 0, generally only used for multiple items. Similar to SelectedIndex, but SelectedIndices can only retrieve indices. |
SelectedItem | Gets the currently selected item in the list. Note that what is obtained is the text content of the selected item in the list, while SelectedIndex and SelectedIndices only obtain the indices of the selected items (int). |
SelectedItems |
Gets the collection of selected items. Use SelectedItems[i] to retrieve the text content of the selected items, where i is the index of the selected items collection. SelectedItems is similar to SelectedIndex, but one retrieves the text field while the other retrieves the index. |
Sorted | Used to set or get whether the list is sorted alphabetically (bool). |
Text | Gets or searches for the text of the currently selected item in the list control. |
ItemsCount | Used to get the current number of items in the list. |
Adding, Inserting, or Deleting Content in ListBox
This is done by using the Items property. Create a new window in Visual Studio, drag in a list control, and set the control name to listBox1.
listBox1.Items.Add("Text of the item to add"); //This will add at the end of the list listBox1.Items.Insert(i, "Text of the item to add"); //This will insert a new item after the item at index i listBox1.Items.Remove("Enter the text of the item you want to remove here"); //Deletion is done using the text content, not the index listBox1.Items.Clear(); //Clear all items in the list listBox1.ClearSelected(); //Deselect all selected items, making them unselected
Deleting Multiple Items in ListBox
Many friends trip here, as VS often prompts that the array is out of bounds. Since deletion in ListBox needs to specify the text of an item and can only delete one item at a time, after deleting one item, the indices will change, which may confuse beginners. Here are two methods of deletion for beginners' reference.
for (int i=listBox1.SelectedItems.Count-1;i>=0 ;i--)
{ //Delete from the back
listBox1.Items.Remove(listBox1.SelectedItems[i]);
//SelectedItems directly retrieves the text of the selected item.
}
//If you are a beginner and cannot understand the code above, don’t worry, skip it, look at the content below, and it will be explained in detail later
for (int i = listBox1.SelectedItems.Count-1; i >=0; i--) { //Also delete from the back
listBox1.Items.Remove(listBox1.Items[listBox1.SelectedIndices[i]]);
//First get the index, then get the text content
}
Getting the Count of the Collection
int i = 0; i=listBox1.Items.Count;
//Get the total count of items in the list i = listBox1.SelectedIndices.Count;
//Get the count of selected items i = listBox1.SelectedItems.Count;
//Get the count of selected items //listBox1.SelectedIndex cannot retrieve the count! Please refer to the property table for the keyword "collection". Only properties capable of retrieving collections can get the count.
Getting the Content of All Selected Items
Create another TextBox control with the name textBox1, with the following code:
textBox1.Clear();
textBox1.Text = "The content of the selected items is:\r\n";
for(int i=0;i<listBox1.SelectedItems.Count ;i++ )
{
textBox1.Text += +listBox1.SelectedItems[i].ToString()+"\r\n";
//Read the content of the selected items one by one
}
ListBox Single Selection or Multiple Selection
Click the ListBox control, find the SelectionMode property in the property panel, which can also be set programmatically. The description of the properties is as follows:
SelectionMode.None indicates that no selection is allowed.
SelectionMode.One indicates that only one item can be selected by the user.
SelectionMode.MultiExtended indicates that multiple items can be selected, but the selected items must be contiguous (adjacent). After selecting one item, users can hold the keyboard's up/down keys to select a range of options, but they cannot select items that are not adjacent. As shown in the figure below:
SelectionMode.MultiSimple indicates that multiple items can be selected, allowing any number of entries to be selected. Clicking once selects an item, and clicking again cancels the selection. As shown in the figure below:
文章评论