简体   繁体   中英

How to show the numeric position from a selected item in a listbox in a label

Sorry for the confusing title but here is my situation: When I select an item from a listbox I want a label showing that item's number. For example if I choose the fifth item in the listbox the label should show "Item number 5 is selected".

How do I do this?

This is simple. All you need to do to get the index of the selected item in the listbox is to use the SelectedIndex property which returns the zero-based index of the selected item. If you want a one-based index instead, just add 1 to the index.

int index = listBox.SelectedIndex;
label.Text = $"Item number {index + 1} is selected.";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM