Results 1 to 9 of 9

Thread: [RESOLVED] ListView ItemSelectedChange Null Reference

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Location
    Colorado
    Posts
    69

    Resolved [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:
    1. Private Sub listColors_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles listColors.ItemSelectionChanged
    2.         clrPreview.BackColor = listColors.FocusedItem.BackColor
    3.     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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Location
    Colorado
    Posts
    69

    Re: ListView ItemSelectedChange Null Reference

    Thanks for the tip, jmcilhinney! I ended up switching to the SelectIndexChanged event and looping through SelectedItems:
    vb Code:
    1. Private Sub listColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listColors.SelectedIndexChanged
    2.     For Each Color As ListViewItem In listColors.SelectedItems
    3.         If Color.Focused Then
    4.             clrPreview.BackColor = Color.BackColor
    5.         End If
    6.     Next
    7. End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Location
    Colorado
    Posts
    69

    Re: ListView ItemSelectedChange Null Reference

    I see... I tried this then:
    vb Code:
    1. Private Sub listColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listColors.SelectedIndexChanged
    2.     clrPreview.BackColor = listColors.SelectedItems(0).BackColor
    3. End Sub
    But got this when one item is selected and I try to select another:
    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ListView ItemSelectedChange Null Reference

    It's not because you're using SelectedIndexChanged. It's for exactly the same reason as I gave earlier:
    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).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Location
    Colorado
    Posts
    69

    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:
    1. Private Sub listColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listColors.SelectedIndexChanged
    2.     Try
    3.         clrPreview.BackColor = listColors.SelectedItems(0).BackColor
    4.     Catch ex As Exception
    5.     End Try
    6. End Sub
    One final question: is it appropriate to leave the catch portion empty if I know what the problem will be?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Location
    Colorado
    Posts
    69

    Re: ListView ItemSelectedChange Null Reference

    vb Code:
    1. Private Sub listColors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listColors.SelectedIndexChanged
    2.     If listColors.SelectedItems.Count > 0 Then
    3.         clrPreview.BackColor = listColors.SelectedItems(0).BackColor
    4.     End If
    5. End Sub
    Thanks for all your help! I really appreciate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width