Click here to Skip to main content
15,886,778 members

Counting ListView items, SelectedItems returns wrong value

Wajrak_ asked:

Open original thread
Hello good people,

I need someone to tell me that I have not lost My sanity, why? Because what I am going to ask about is just silly. Ok here we go:

I have one ListView that is getting populated with file names. I want it to use multiple selection but like in "Windows Commander" by simply using up/down arrow keys and selecting rows by simply hitting space. I have created Key Up event catching space button being released, in that even Im getting index of currently selected (highlighted) row and adding it to my List<int>. Everything works like a charm.

I have created another event for my ListView, ItemSelectionChanged and in this Event I have a little loop:

C#
private void listViewLeft_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
    if (!e.IsSelected)
    {
        foreach (int selectedIndex in multipleSelectionArray)
        {
            Console.WriteLine(selectedIndex.ToString());
            listViewLeft.Items[selectedIndex].Selected = true;
        }
    }
}


All peachy but whenever I'm trying to use listViewLeft.SelectedItems.Count it returns 0 or 1, randomly. The best thing is if I select rows using ctrl and mouse it works like a charm.
What am I missing? Whats the difference between listViewLeft.Items[selectedIndex].Selected = true and selecting rows by using mouse?

Don't get me wrong I can work around it simply by counting items in my List<int> but it just annoys me because I never had a problem like this.

Thank you for your time and I do apologize if I over complicated things ;)
Wajrak
Tags: C#, ListView

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900