none
ListView - Selecting an Item Programmatically RRS feed

  • Question

  • Hey all,



    This should be a simple query, but I've spent a few hours trying to resolve it without any success Sad



    Ok, I've got 3 items in my ListView. The middle one is selected. What I want to do is select the 1st item. I am able to retrieve the currently selected item. I thought it would be a case of doing ListView.Items[SelectedIndex-1].Selected = true; (and then doing the select/focus stuff after...).



    However whenever I do call Items[Index].Selected = true, I get a NullReferenceException being raised. If I set the statement to equal false, then the exception doesn't get raised - any ideas why I'm getting this Null Exception? I've also gone to the lengths of retrieving the item to ensure its not set to NULL, and then trying to call the Selected method - same behaviour...?



    I have noticed that if I runover this error in the debugger, the item is selected correctly.



    Cheers,


    Jock



    Sample code: (please note this is lengthier than it should be - due to debugging to see whats going on!)


               // Retrieve the currently selected item index

             int iIndex = GetSelectedItemIndex();

                int iUnselectedCount = 0;
                int SelectIndex = 0;

                // iterate through the list and check how many items are //selected/unselected... this proves i have no problem access the item when its //unselected
                for (int iLoop = 0; iLoop < ListViewMessages.Items.Count; iLoop++)
                {
                    if (ListViewMessages.Items[iLoop].Selected == true)
                    {

                        if (iLoop > 0)

                        {

                              SelectIndex = iLoop - 1;

                        }

                        break;
                    }
                    else
                    {
                        iUnselectedCount++;
                    }
                }

              // check the item I'm about to select isn't null
                if (ListViewMessages.Items[SelectIndex] != null)
                {

                    // not null, so select it - THIS LINE RAISES THE NULL EXCEPTION!?

                    ListViewMessages.Items[SelectIndex].Selected = true;
                }

                ListViewMessages.Select();


    Thursday, April 19, 2007 9:27 AM

Answers

  • Thanks for the reply. I'll continue investigating. I think the code looks ok aswell.



    As for the -1, the Index is the selected item, I want to select the item before.

    Thursday, April 19, 2007 12:28 PM

All replies

  • I suspect the error is somewhere else, because that code should run ok.

    Incidentally, the line "SelectIndex = iLoop - 1;" looks s bit suspect. Why are you subtracting one from the index?
    Thursday, April 19, 2007 12:16 PM
  • Thanks for the reply. I'll continue investigating. I think the code looks ok aswell.



    As for the -1, the Index is the selected item, I want to select the item before.

    Thursday, April 19, 2007 12:28 PM