CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2001
    Location
    AZ
    Posts
    201

    [RESOLVED] Problem with Listview SelectedIndexChanged event handler

    I set up an event handler to catch when the user clicks on an item in a listview. The first time an item is clicked on, the SelectedIndices contains the index of the item selected and SelectedIndices.Count has a value of 1. After the first time when the event handler fires the count always has a value of zero and the SelectedIndices is always zero.

    Thanks in advance,
    Hal
    up·grade (up'gräd'),
    to take out old bugs
    and put in new ones.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem with Listview SelectedIndexChanged event handler

    Is the list control style set to multi-select?

    If not, use the SelectedIndex property to retrieve the index to the selected item.

  3. #3
    Join Date
    Feb 2001
    Location
    AZ
    Posts
    201

    Re: Problem with Listview SelectedIndexChanged event handler

    Thank you for replying. The style is single select. I don't see a SelectedIndex property. Just SelectedIndicies. This is what MSDN says about the SelectedIndicies property:

    When the MultiSelect property is set to true, this property returns a collection containing the indexes of all items that are selected in the ListView. For a single-selection ListView, this property returns a collection containing a single element containing the index of the only selected item in the ListView.
    up·grade (up'gräd'),
    to take out old bugs
    and put in new ones.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem with Listview SelectedIndexChanged event handler

    Ok. I was looking at the ListControl class instead of the ListView class.

    Post your the code for the SelectedIndexChanged handler.

  5. #5
    Join Date
    Feb 2001
    Location
    AZ
    Posts
    201

    Re: Problem with Listview SelectedIndexChanged event handler

    selectedProcessRowNumber is defined as:
    private int selectedProcessRowNumber;


    private void lstvwAllProcesses_SelectedIndexChanged(object sender, EventArgs e)
    {
    selectedProcessRowNumber = lstvwAllProcesses.SelectedIndices[0];
    }
    up·grade (up'gräd'),
    to take out old bugs
    and put in new ones.

  6. #6
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Problem with Listview SelectedIndexChanged event handler

    Strange... As far as I can see, there's no reason for this to happen. There must be some code interaction you're not aware of, that is not shown here. Are there other list views on your window? That could accidentally be set to use this event handler? (But still...) Or, maybe you accidentally unhooked lstvwAllProcesses from lstvwAllProcesses_SelectedIndexChanged and the event is actually fired by something else? (Seems unlikely, but that's just of the top of my head - you didn't provide an awful lot of detail.)

    Make a quick, small test app with just a list view and see if it works properly. If it does, than it's something in your code: examine anything you think that could be relevant, and go step-by-step in the debugger, replicating the scenario, check every var (selectedProcessRowNumber, lstvwAllProcesses, sender).
    Last edited by TheGreatCthulhu; October 11th, 2011 at 11:32 PM.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem with Listview SelectedIndexChanged event handler

    Are you by chance hooking up more than one event handler?

    E.g.
    Code:
    lstvwAllProcesses.SelectedIndexChanged += lstvwAllProcesses_SelectedIndexChanged;
    Sometimes folks incorrectly hook up the handler more than once and it causes strange things to happen.

  8. #8
    Join Date
    Feb 2001
    Location
    AZ
    Posts
    201

    Re: Problem with Listview SelectedIndexChanged event handler

    I have attached a test program. The form has one listview and nothing else.
    I get the same results. The first time I click on an item the selected index is returned. The second time I get the error.

    Thanks for the help.
    Hal
    Attached Files Attached Files
    up·grade (up'gräd'),
    to take out old bugs
    and put in new ones.

  9. #9
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Problem with Listview SelectedIndexChanged event handler

    Figured it out. As it turns out, the SelectedIndexChanged event is fired more times then you'd intuitively expect. When you switch from a previously selected item to the next one, it's done in two steps: in the first one, the selected state changes from something to nothing, SelectedIndexChanged fires, and the SelectedIndices returns an empty collection (as nothing in is currently selected); in the second step, the new item get's selected, the SelectedIndexChanged event is fired again, and this time there will be an entry on SelectedIndices[0].

    Microsoft kinda-sorta mentions this in the documentation:
    A ListView.SelectedIndexCollection that contains the indexes of the selected items. If no items are currently selected, an empty ListView.SelectedIndexCollection is returned.
    Hm... they should really elaborate on that a bit more...

    Anyway, it's an easy fix:
    Code:
    if (listView1.SelectedIndices.Count > 0)
        selectedItem = listView1.SelectedIndices[0];
    If you worked with a multiselect listview, you could use foreach instead, as it will do nothing if the collection is empty.

  10. #10
    Join Date
    Feb 2001
    Location
    AZ
    Posts
    201

    Re: Problem with Listview SelectedIndexChanged event handler

    It works fine now. Thanks for all the assistance. I really appreciate it.

    Regards,
    Hal
    up·grade (up'gräd'),
    to take out old bugs
    and put in new ones.

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

Featured