Thursday, March 29, 2012

list box no selection

if(listBox.SelectedItem.Text != null)
{
// test
}

This code gives exception if no item is selected in the list box, how can I
check if any item is selected or not?try testing SelectedItem for null instead, before using the property.

JIM.H. wrote:

Quote:

Originally Posted by

if(listBox.SelectedItem.Text != null)
{
// test
}
>
This code gives exception if no item is selected in the list box, how can I
check if any item is selected or not?


"JIM.H." <JIMH@.discussions.microsoft.comwrote in message
news:74BB25D8-D47E-468C-93BC-BF1B57D7E044@.microsoft.com...

Quote:

Originally Posted by

if(listBox.SelectedItem.Text != null)
{
// test
}
>
This code gives exception if no item is selected in the list box, how can
I
check if any item is selected or not?


if(listBox.SelectedIndex == -1)
{
// no items selected
}

0 comments:

Post a Comment