I’m 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 function is get executed twice.and the message box shows empty string.
Why is it happening? How can I avoid that?
try construction like this:
listBox1.SelectedIndex is calling SelectionChanged event so this function is called twice.