简体   繁体   中英

how to exchange selected item in a list box winform

i'm trying to change 'selectedItem' in the list box, but 'selectedItem' is staying as is even though i create a new item with different data. appreciate your help

this.listBox1.SelectedItem = new ListBoxItem(m_CurrentItem);

//next line operate the event list item changed
this.listBox1.Items[index] = this.listBox1.SelectedItem;  

I think you're doing the operations in the reverse order ^^

You should first add the new item in the list box with

this.listBox1.Items.Add(YourNewItem);

Then you can select the newly inserted item with

this.listBox1.SelectedItem = YourNewItem;

Or, since the .Add Method adds the element in the last position of the Items array, you can use

this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1

See this article for reference on the ListBox.SelectedItem Property: http://msdn.microsoft.com/it-it/library/system.windows.forms.listbox.selecteditem(v=vs.110).aspx :)

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