简体   繁体   中英

How to get text from selected line in ListBox (c#)?

I want to get data from selected line in List Box. I use command:

string selected = ListBox1.SelectedItems[0].ToString(); 

But the result is:

ListVievItem: {here is correct value}

What should i do with this: "ListVievItem: {}"

EDIT As suggested by John Willemse, ListBox can't have ListViewItems so it seems like this question relates to ListView rather than to a ListBox so the code in the answer is changed accordingly.

When you call it like this listView1.SelectedItems[0].ToString(); you are actually calling the ToString() method of the ListViewItem object which gives the unwanted result (first prints the class name and then the value). Each ListViewItem object has Text property from which you can get its text.

string selected = listView1.SelectedItems[0].Text; 

尝试这样的事情:

string selected = ListBox1.SelectedItems[0].Text;

您是否尝试过: ListBox1.SelectedItem.Value

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