Use of ListBox control:
1) Control properties
Items
SelectedItems
SelectioModes
2) Data binding
DataSoure
DisplayMember
ValueMenber
3) Example
The following will begin to explain the use of the ListBox control above one by one.
First, let’s talk about the properties of the control.
(1)Items: Use this property to get the properties of the list control item. This property can be used to determine the selected items in the list control. When adding items, you can either add them statically during design or dynamically in the code. If you do not want to display items added at the design time, you can add this.listBox1.Items.Clear() in your code; only the options added in your code are displayed.
(2) SelectedItems: Get the collection containing the currently selected items in the ListBox.
(3)SelectioModes: Get or set the method used to select items in ListBox. There are four values to choose; the default is SelectionMode.One, and only one can be selected; the property value is none cannot be selected; when the property MultiExtended is pressed, click the mouse while clicking the Shift key or one of the Shift key and arrow keys (up, down, left and right) at the same time, the selected content will be expanded from the previous selected item to the current item. Pressing the Ctrl key while clicking the mouse will select or unselect an item in the list; when the property is set to MultiSimple, clicking or pressing the space bar will select or unselect an item in the list.
Next, let’s talk about data binding. Usually the data is variable, so data binding is required. There are several types of data binding. One is to bind the DataTable or DataSet obtained from the database; the other is to customize a class to bind the data in the custom class. There is a little difference between data binding in C# and data binding in ASP.NET. After binding data in ASP.NET, a DataBind method must be called, but it is not necessary in C#. DisplayMember gets or sets the properties to be displayed.
Finally, let’s do an example as shown in the figure:
Let’s just talk about the implementation of several important methods, so the specific code will not be written here. Put the options in the left border into the right border. Code:
The code copy is as follows:
for (int i = this.listBox1.SelectedItems.Count - 1; i >= 0;i -- )
{
Menu menu = (menu)this.listBox1.SelectedItems[i];
this.listBox2.Items.Add(menu);
this.listBox1.Items.Remove(i);
}
Although the above function has been implemented, there is a problem, that is, the item on the left becomes reverse order after reaching the right. This is the code we need to write the add and move out separately, the code:
The code copy is as follows:
for (int i = 0; i < this.listBox1.SelectedItems.Count;i++ )
{
Menu menu = (menu)this.listBox1.SelectedItems[i];
this.listBox2.Items.Add(menu);
}
for (int i = this.listBox1.SelectedItems.Count - 1; i >= 0; i--)
{
this.listBox1.Items.Remove(i);
}
So far, there is another problem that when selecting the first few items in the box, there is no problem to move the right, but when selecting the next few items, the next few items are moved to the right box, and the first few items are moved out of the left box. The reason for this problem is that we will
SelectedItems and Items are confused. This is a mistake that many beginners are prone to make. Code: