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
Netflix is worth more than Disney. How the Mouse can bounce back
High page file usage
Accuweather 15 Day Weather Forecast Philadelphia
Mtlsd.schoology
Megan Officer
Abbytheedoll
Detection of GM Canola MS11, DP-073496-4, and MON88302 events using multiplex PCR coupled with capillary electrophoresis
The Salem News Obituaries
Washington Food Handlers Card Test Answers
Central Mn Credit Union Perham
Gpa Calculator Georgia Tech
Home.depot.gift Card Balance
Dragon Block C Resource Pack
Espn Masters Leaderboard
Relics of Rivellon – Armor Stats & Checklist per Act – Steam Solo
Can Kool Aid Powder Go Bad
10-Day Weather Forecast for Nockamixon Township, PA - The Weather Channel | weather.com
Anime Feet Blogspot
Magellan Outdoors Men's Luther Ii Boots
Lima Crime Stoppers
„The Boogeyman“: Kritik zum Kinostart
Pizza Hut Express, 3308 N Dinuba Blvd, Visalia, CA 93291, US - MapQuest
A Far Eastern Yarn Ffxiv
Boyfriend (2018) | KDrama Recaps on Dramabeans
Craigslist Ocala Garage Sales
Dunkelbergers Gun Inventory
Milton Shealy Funeral Home Obituaries
Edenmodelsva
Igloocar/Dennison semirremolques con caja de lona from de neerlandés venta
Larue County Pva
Best things to do in Newport, Rhode Island - Lonely Planet
The Year The Internet Stopped Laughing
Sarah Colman-Livengood Park Raytown Photos
Botw Royal Guard
E 77 · Baureihe 177 · bay. EG3 · preuß. EG 701–25 Fotos
Bustadice Review (2024) | Bonus Codes | Strategy | Scripts
Methodist Laborworkx
Chase Bank Hours Drive Thru
Palm Beach Tan Nashville
Beth Israel Farr Building Parking
Colorado Pick 3 Lottery
Straightup Internet Hotspot Pass
Leanbeefpatty tit*
Rubmd Columbus Ohio
Igumdrop Deepfake
Bootyexpo Net
2003 Chevrolet Corvette Z06 Coupe On for sale - Portland, OR - craigslist
Miraheze Awful Movies Wiki
Ter Review
What is "Brrr skibidi dop dop / dom dom yes yes"? Memes, explanation, meaning, definition - Bedeutung Online
Ctip General Awareness Quiz Answers 2022
Linkedin.comnk
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.