简体   繁体   中英

Accessing Selected Index of ListBox

I'm attempting to access a list called people and I'm matching up the index of the listitem selected to the index of my list so that I can print the surname of the person to a textbox. I have this code:

surnameTxtBox.Text = people[listBoxNames.SelectedItems[0].Index].Surname;

listBoxNames is the name of my ListBox but for some reason, visual studio tells me that there's no definition called index?

您需要SelectedIndex来获取SelectedItem索引:

surnameTxtBox.Text = people[listBoxNames.SelectedIndex].Surname;

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