3

I've got a listbox in C# that's dynamically filled. I've got a problem with the selectedindex_changed of the listbox. Everytime it gives the value of the last item in the listbox and not the one I selected. I've got that same problem with a combobox on another page.

I don't know why this happens this way. Does anyone knows what I'm doing wrong?

First I make a new object Item to put text with the ID as the value in the listbox.

    public class Item
    {
        public string Text { get; set; }
        public int Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }

Here I fill the listbox with the text and the ID as its value. This works fine, the listbox gets filled as it should be.

private void FormDeelnemers_Load(object sender, EventArgs e)
    {
        BLPersoon blPersoon = new BLPersoon();
        DBOpdracht.PersoonDataTable personen = blPersoon.GetAllPersonen();

        //Item item = new Item(); -> edit: delete this

        foreach (DBOpdracht.PersoonRow persoon in personen)
        {
            Item item = new Item(); -> edit: add this here
            item.Text = persoon.naam;
            item.Value = persoon.ID;
            listBoxPersonen.Items.Add(item);
        }
    }

Here is the problem. It gives the value of the last item in the listbox and not the one I've selected. How do I get the selected one?

    private void listBoxPersonen_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nummer = (listBoxPersonen.SelectedItem as Item).Value;
        MessageBox.Show(nummer.ToString());
        //MessageBox.Show(listBoxPersonen.SelectedItem.ToString()); -> same problem
    }
Alvin Wong
  • 12,210
  • 5
  • 51
  • 77
Lewis
  • 225
  • 1
  • 6
  • 18
  • Do you have your list to cause a postback when the selection is changed? Or are you using some other control to cause a postback? The issue seems to be the timing when the server actually receives information from the client that the selected item has changed. – Geeky Guy May 07 '13 at 21:09
  • No, the code you see in the selectedindexchanged is the only code I've got so far, because I stumbled on this problem. With my combobox I've got the same problem and when the messagebox pops up, the last item is selected but when I close the messagebox, the item I selected is selected again. So that's also weird I think. – Lewis May 07 '13 at 21:15
  • 3
    Your `Item` instantiation is outside of your `foreach` loop in `FormDeelnemers_Load`. –  May 07 '13 at 21:30
  • Awesome it works, thanks! I know I already tried that with a few other things and then I always got the value of the first item but now it seems to work properly. Thanks alot! – Lewis May 07 '13 at 21:35
  • @bmused, please, post it as an answer. – skink May 09 '13 at 10:06

1 Answers1

0

Thanks to @bmused who solved it in the comments, but could anyone explain what happened exactly?

i had the same problem with a combobox where i was adding items using a foreach. the question is what does the instantiation of an item during the insertion have to do with selecting from a combobox?

P.S. Sorry admins couldn't post this as comment.

BlackCoffee
  • 375
  • 2
  • 10