Using c-sharpcorner.com/UploadFile/mahesh/listbox-in-C-Sharp/ > "C# ListBox Selection Mode and Selecting Items" >
listBox1.SelectionMode = SelectionMode.MultiSimple;
listBox1.SetSelected(1, true);
listBox1.SetSelected(2, true);
Resuts in error messages
'SelectionMode' does not contain a definition for 'MultiSimple'
ListBox' does not contain a definition for 'SetSelected'
How do I highlight one ListBox item? See snipboard.io/F2wAt5.jpg.
Tuhin PaulPosted Mar 9, 2024, 7:45 PM
This code will clear any existing selections in the ListBox and then highlight the item at the specified index (in this case, index 1). Remember that indexing in C# starts from 0, so if you want to highlight the first item, you would use index 0.
If you're looking to allow multiple selections, then you would indeed set the SelectionMode property, but the correct value is
SelectionMode.MultiSimple, notMultiSimple.Tuhin PaulPosted Mar 9, 2024, 7:45 PM
To highlight a single item in a ListBox in C#, you don't need to set the SelectionMode property. Instead, you can manipulate the SelectedIndex property to achieve what you want.