SelectionMode of ListBox control in C#

We can set the selection mode of ListBox to One, Multisimple and MultiExtended in C#

The SelectionMode property enables us to determine how many items in the ListBox a user can select at one time and how the user can make multiple-selections. When the SelectionMode property is set to SelectionMode.MultiExtended, pressing SHIFT and clicking the mouse or pressing SHIFT and one of the arrow keys (UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT ARROW) extends the selection from the previously selected item to the current item. Pressing CTRL and clicking the mouse selects or deselects an item in the list. When the property is set to SelectionMode.MultiSimple, a mouse click or pressing the SPACEBAR selects or deselects an item in the list.

We can set the different selection mode at design time through SelectionMode property:

Following example show how to set the multiple items in ListBox control at run time

Code:

 
   private void ListBoxSelectionMode()
{
	ListBox1.Items.Add("New Delhi");
	ListBox1.Items.Add("Tokyo");
	ListBox1.Items.Add("Newyork");
	ListBox1.Items.Add("Sydney");
	ListBox1.Items.Add("Paris");
	ListBox1.Items.Add("Londan");
 
	ListBox1.SelectionMode = SelectionMode.MultiExtended;
 
	ListBox1.SetSelected(0, true);
	ListBox1.SetSelected(1, true);
	ListBox1.SetSelected(3, true);
}

One thought on “SelectionMode of ListBox control in C#”

Comments are closed.