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:

  1.  
  2.    private void ListBoxSelectionMode()
  3. {
  4.  ListBox1.Items.Add("New Delhi");
  5.  ListBox1.Items.Add("Tokyo");
  6.  ListBox1.Items.Add("Newyork");
  7.  ListBox1.Items.Add("Sydney");
  8.  ListBox1.Items.Add("Paris");
  9.  ListBox1.Items.Add("Londan");
  10.  
  11.  ListBox1.SelectionMode = SelectionMode.MultiExtended;
  12.  
  13.  ListBox1.SetSelected(0, true);
  14.  ListBox1.SetSelected(1, true);
  15.  ListBox1.SetSelected(3, true);
  16. }

facebooktwittergoogle_plusredditpinterestlinkedinmail

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>