Search code examples
c#listviewcountassembly-references

C# ListView SelectedIndices and SelectedItems Count Issue


Having some trouble with these methods on C# 2010 express with a new winform project both data types have the method count, which seems to be documented on MSDN, however I can not seem to get them to work. The listview control itself seems fine when compiling.

 listView2.SelectedItems.Count();

Error 1 'System.Windows.Forms.ListView.SelectedListViewItemCollection' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Windows.Forms.ListView.SelectedListViewItemCollection' could be found (are you missing a using directive or an assembly reference?)

listView2.SelectedIndices.Count();

Error 1 'System.Windows.Forms.ListView.SelectedIndexCollection' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Windows.Forms.ListView.SelectedIndexCollection' could be found (are you missing a using directive or an assembly reference?)

Both data types seem to be defined. Also cannot use indices.

 listView2.SelectedItems[0] 

Solution

  • They are properties, not methods:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selectedlistviewitemcollection.aspx

    http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selectedindexcollection.aspx

    Remove the brackets:

    var count = listView2.SelectedItems.Count;
    count = listView2.SelectedIndices.Count;
    

    You can use index notation on them. The SelectedItems property exposes a string and int index. The SelectedIndices property only exposes an int index.