list box no selection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top