[RESOLVED] ListView ItemSelectedChange Null Reference
I have a ListView populated with colors one can choose to customize the UI a bit. I want the user to be able to click an item in the ListView and have it's BackColor become the BackColor of a picturebox (the "preview"). This is my code:
VB.NET Code:
Private Sub listColors_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles listColors.ItemSelectionChanged
clrPreview.BackColor = listColors.FocusedItem.BackColor
End Sub
If I try to select a different item with one already selected, I get "System.NullReferenceException was unhandled." How can I fix this?
Re: ListView ItemSelectedChange Null Reference
First up, you shouldn't be using the FocusedItem property:
Quote:
Originally Posted by MSDN
Use the SelectedItems or SelectedIndices properties to obtain the selected items in the ListView control, the FocusedItem property is not necessarily selected.
As for the question, that event is going to be raised twice each time the selection changes: once to deselect the old item and once to select the new item. Obviously it's the second one you're interested in.
Re: ListView ItemSelectedChange Null Reference
Thanks for the tip, jmcilhinney! I ended up switching to the SelectIndexChanged event and looping through SelectedItems:
vb Code:
Private Sub listColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listColors.SelectedIndexChanged
For Each Color As ListViewItem In listColors.SelectedItems
If Color.Focused Then
clrPreview.BackColor = Color.BackColor
End If
Next
End Sub
Re: ListView ItemSelectedChange Null Reference
That code still doesn't make sense. Either you want the item that's focused or you want the item that's selected. They may be one and the same but they may not be. Your code will only detect an item that is both focused and selected. I'm guessing that that is not what you're trying to do. I'm guessing that you actually want the item that's selected. In that case you just want the first item in the SelectedItems collection, irrespective of whether it has focus or not.
Re: ListView ItemSelectedChange Null Reference
I see... I tried this then:
vb Code:
Private Sub listColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listColors.SelectedIndexChanged
clrPreview.BackColor = listColors.SelectedItems(0).BackColor
End Sub
But got this when one item is selected and I try to select another:
Quote:
System.ArgumentOutOfRangeException was unhandled: InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index
Is it because I'm using SelectedIndexChanged? Which event is more appropriate?
Re: ListView ItemSelectedChange Null Reference
It's not because you're using SelectedIndexChanged. It's for exactly the same reason as I gave earlier:
Quote:
that event is going to be raised twice each time the selection changes: once to deselect the old item and once to select the new item. Obviously it's the second one you're interested in.
The first time the event is raised there are no selected items so there is no SelectedItems(0).
Re: ListView ItemSelectedChange Null Reference
Ah, I must have skipped right over that. :P I ended up wrapping it in a try block:
vb Code:
Private Sub listColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listColors.SelectedIndexChanged
Try
clrPreview.BackColor = listColors.SelectedItems(0).BackColor
Catch ex As Exception
End Try
End Sub
One final question: is it appropriate to leave the catch portion empty if I know what the problem will be?
Re: ListView ItemSelectedChange Null Reference
That is a very bad approach. You should never just do something and then catch the exception when it fails if you can easily pre-validate and avoid the exception altogether, which you can in this case. You only want to get an item if there's an item to get, right? So why not just check first whether there's an item to get? You can't get an item if the SelectedItems collection is empty, so that's what you should be checking for.
Re: ListView ItemSelectedChange Null Reference
vb Code:
Private Sub listColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listColors.SelectedIndexChanged
If listColors.SelectedItems.Count > 0 Then
clrPreview.BackColor = listColors.SelectedItems(0).BackColor
End If
End Sub
:D Thanks for all your help! I really appreciate it.