1

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;  
2
  • You could try selected index, like: listBox1.SelectedIndex = integer here;
    – Max
    Mar 21, 2014 at 10:36
  • 1
    use SelectedIndex: listBox1.Items[listBox1.SelectedIndex] = m_CurrentItem Mar 21, 2014 at 10:45

1 Answer 1

0

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.