ListBox.SelectedIndex Property (System.Windows.Forms) (2024)

Table of Contents
Examples Remarks Applies to See also

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

public: virtual property int SelectedIndex { int get(); void set(int value); };
[System.ComponentModel.Bindable(true)][System.ComponentModel.Browsable(false)]public override int SelectedIndex { get; set; }
[<System.ComponentModel.Bindable(true)>][<System.ComponentModel.Browsable(false)>]member this.SelectedIndex : int with get, set
Public Overrides Property SelectedIndex As Integer

Property Value

A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.

Attributes

Exceptions

The assigned value is less than -1 or greater than or equal to the item count.

The SelectionMode property is set to None.

Examples

The following code example demonstrates how to use the SelectedIndex property with the TopIndex property to move the currently selected item to the top of the list of items in the display area of the ListBox. The example further demonstrates how to remove items using the RemoveAt method of the System.Windows.Forms.ListBox.ObjectCollection class, and how to clear all item selection using the ClearSelected method. The code first moves the currently selected item in the ListBox to the top of the list. The code then removes all items before the currently selected item and clears all selections in the ListBox. This example requires that a ListBox containing items is added to a form and that an item is currently selected in the ListBox.

private: void RemoveTopItems() { // Determine if the currently selected item in the ListBox // is the item displayed at the top in the ListBox. if ( listBox1->TopIndex != listBox1->SelectedIndex ) // Make the currently selected item the top item in the ListBox. listBox1->TopIndex = listBox1->SelectedIndex; // Remove all items before the top item in the ListBox. for ( int x = (listBox1->SelectedIndex - 1); x >= 0; x-- ) { listBox1->Items->RemoveAt( x ); } // Clear all selections in the ListBox. listBox1->ClearSelected(); }
private void RemoveTopItems(){ // Determine if the currently selected item in the ListBox // is the item displayed at the top in the ListBox. if (listBox1.TopIndex != listBox1.SelectedIndex) // Make the currently selected item the top item in the ListBox. listBox1.TopIndex = listBox1.SelectedIndex; // Remove all items before the top item in the ListBox. for (int x = (listBox1.SelectedIndex -1); x >= 0; x--) { listBox1.Items.RemoveAt(x); } // Clear all selections in the ListBox. listBox1.ClearSelected();}
Private Sub RemoveTopItems() ' Determine if the currently selected item in the ListBox ' is the item displayed at the top in the ListBox. If listBox1.TopIndex <> listBox1.SelectedIndex Then ' Make the currently selected item the top item in the ListBox. listBox1.TopIndex = listBox1.SelectedIndex End If ' Remove all items before the top item in the ListBox. Dim x As Integer For x = listBox1.SelectedIndex - 1 To 0 Step -1 listBox1.Items.RemoveAt(x) Next x ' Clear all selections in the ListBox. listBox1.ClearSelected()End Sub

Remarks

For a standard ListBox, you can use this property to determine the index of the item that is selected in the ListBox. If the SelectionMode property of the ListBox is set to either SelectionMode.MultiSimple or SelectionMode.MultiExtended (which indicates a multiple-selection ListBox) and multiple items are selected in the list, this property can return the index to any selected item.

To retrieve a collection containing the indexes of all selected items in a multiple-selection ListBox, use the SelectedIndices property. If you want to obtain the item that is currently selected in the ListBox, use the SelectedItem property. In addition, you can use the SelectedItems property to obtain all the selected items in a multiple-selection ListBox.

Applies to

See also

ListBox.SelectedIndex Property (System.Windows.Forms) (2024)
Top Articles
6 Gifts You Shouldn't Give on Chinese New Year - Claverley Court Apartments
Reasons Why Your Customers Won't Leave Reviews
Cache County Incident Blotter
Patelco New Car Loan Rates
Grammy Winner Lipa Wsj Crossword Clue
Julian Sands Shirtless
One Fine Chocolate Place
Type of Funeral Homes
Temu Codes 2022
Rocklin Studio Movie Grill Menu
Darkycoupons
Rauw Alejandro New Song 2022
Olivia Dunne 2023: Puerto Rico
Top Songs On Octane 2022
Tesla Holiday Update 2023.44.30.1 Includes FSD Beta 11.4.9 - TeslaNorth.com
Papa Johns Pizza Hours
Https //Epy.prismhr.com/Epy/Auth/#/Log In
What Are Lash Extensions and How Are They Applied?
Uwsa 1 Step 3
Las Vegas Noaa
Espn Auction Values
Tampa Bay Times Mugshots
Pdq Menu Nutrition Facts
Ltlv Las Vegas
Saybyebugs At Walmart
Studentvue Lake Havasu
Northeast Ob Gyn Associates Quarry
Mendoza Clinic Pharmacy
Nog Bible
Herriman Heights Dental
Stars Cinema Golden
Panty Note 33
Dallas Cowboys On Sirius Xm Radio
Ezstub Cross Country
H12 Weidian
Complete Growth Inhibition of Pseudomonas aeruginosa by Organo-Selenium-Incorporated Urinary Catheter Material.
Sayuri Pilkey
Made in Abyss (Anime)
Gilman Springs Road Accident Today 2022
Dlnet Delta Com Dlnet
Beacon Schneider Gibson County
Demonologist – Job, Salary, Requirements - Improve Magic
Her Triplet Alphas Chapter 26 Free
Chicago Craigslist Classifieds
Journal articles: 'History of journalistic criticism in Portugal' – Grafiati
The Times Of Trenton Obituaries Today
Bmw 328i e46 - oferte
Linux-Satelliten Receiver-Test: AX 4K-Box HD60 4K UHD E2 Linux im Test
Craigslist St. Paul
Days Calculator 📅 (Days Between Dates)
Blow Dry Bar Boynton Beach
Avalynn_Daniels
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6018

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.