-1

i have problem with clearing selection of listBox. After:

ListBox.ClearSelected();

I receive message about unhandled exception (Object reference not set to an instance of an object).

Does anyone have solution of this problem?

user393031
  • 11
  • 2
  • 1
    ListBox is null. Make sure its not null. Maybe use a null check? – Aron Jan 20 '22 at 12:42
  • 1
    Put a breakpoint on that line and check if "ListBox" is assigned properly. – Rowin Jan 20 '22 at 12:42
  • Thank you for answers, actually really I need null check. In my case it helps to check listBox.SelectIndex != -1 on listBox_SelectedIndexChanged method – user393031 Jan 20 '22 at 13:05

1 Answers1

0

You've not got anything selected in the listbox before calling the ListBox.ClearSelected();

I suggest you do the following:

if(Listbox1.SelectedValue != null){
ListBox.ClearSelected();
}
  • 1
    Thank you, but in my case it helps to check listBox.SelectIndex != -1 on listBox_SelectedIndexChanged method. – user393031 Jan 20 '22 at 13:04