How to retrieve the value selected by the user in a ListBox?

To obtain the value selected by the user in the ListBox, you can use the SelectedIndex property to get the index of the selected item, and then use the Items collection to get the value of the selected item. An example code is provided below:

// 获取ListBox中选定项的索引
int selectedIndex = listBox1.SelectedIndex;

// 获取选定项的值
string selectedValue = listBox1.Items[selectedIndex].ToString();

// 输出选定项的值
Console.WriteLine(selectedValue);

In the code above, assuming listBox1 is a ListBox control, the index of the user-selected item is obtained through the SelectedIndex property. The value of the item corresponding to this index is then retrieved from the Items collection, and finally the value of the selected item is outputted.

 

More tutorials

What are the differences between ListBox and ComboBox?(Opens in a new browser tab)

The similarities and differences of ListBox and DataGridView(Opens in a new browser tab)

convert string to character array in Java.(Opens in a new browser tab)

What is the method for retrieving a timestamp in Linux?(Opens in a new browser tab)

How to retrieve the number of full garbage collections in Java using JMX?(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds