tags:

views:

54

answers:

1

Hi,Im having listbox and getting selected item.The code is

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = listBox1.SelectedIndex;
            string str =""; 
            if (index == 0)
            {
                 str = (string)Text1.Text;
            }
            else if (index == 1)
            {
                 str = (string)Text2.Text;
            }
            else if (index == 2)
            {
                 str = (string)Text3.Text;
            }
            MessageBox.Show(str);
          //  listBox1.SelectedIndex = -1;            
        }

By this above code,after i selected one item,blue color bar is displaying in selected item,To avoid that i gave listBox1.SelectedIndex = -1;. If i gave this this fucntion is get executed twice.and the message box shows empty string. Why its happening?How can i avoid that.

A: 

try construction like this:

listBox1.SelectionChanged -= listBox1_SelectionChanged;
listBox1.SelectedIndex = -1;
listBox1.SelectionChanged += listBox1_SelectionChanged;

listBox1.SelectedIndex is calling SelectionChanged event so this function is called twice.

Steck
Yes..its working.Thank you.
Anu