Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
while (i < lbxcityTours.Items.Count)
                {
                    lbxcityTours.SelectedIndex = i;
                    if (lbxcityTours.SelectedItem.ToString() == t1)
                    {
                        work.Text = lbxcityTours.SelectedIndex.ToString();
                        lbxcityTours.SelectedItem.Value = "0";
                        //remove the selected item from the listbox
                        lstBoxAddedTours.Items.Remove(lstBoxAddedTours.SelectedItem);
                    }
                    i += 1;
                }


What I have tried:

My intention is to change the content of lbxcityTours.SelectedIndex i if it matches string t1
any ideas?
Posted
Updated 7-May-17 3:24am

1 solution

How bizarre! For the benefit of anyone else reading this, with a breakpoint on the line
C#
if (lbxcityTours.SelectedItem.ToString() == t1)
examining the value of lbxcityTours.SelectedIndex gives 0 for all values of i

I have no idea why that is happening, but I managed to stop it from misbehaving by ensuring nothing was selected at all before assigning to the SelectedIndex - i.e.
C#
lbxcityTours.SelectedIndex = -1; //Force the change to SelectedIndex
lbxcityTours.SelectedIndex = i;
Perhaps it's telling that Microsoft do not give an example of setting this value in the documentation, only getting it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900