Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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. Everything works like a charm.
 
I have created another event for my ListView, ItemSelectionChanged and in this Event I have a little loop:
 
        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 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 Wink | ;)
Wajrak
Posted 3-May-13 5:52am
Wajrak_218
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The name of you method suggests that you are handling the event when the selection is changed (never, never use such names, rename auto-generated names to something semantic; and never ask questions like that; always show how you add a handler to an invocation list of an event instance, "+=" operator). Now look what you are doing: you are changing selection inside a selection changed method! This can cause to a pretty complex behavior, but not random. You just did not get that.
 
I don't know what do you want to achieve, but just don't do it. Do something else. Smile | :)
 
—SA
  Permalink  
Comments
Wajrak_ at 3-May-13 11:15am
   
Sergey Alexandrovich I love you ;)
I remember you answering my question last year and you know what? I feel like an idiot now. You answer explains me being unable to debug and trace code in this case. I'm an idiot.
Thank you!
 
BTW, what I was trying to achieve was selecting multiple rows in ListView by simply going up and down using arrow keys and selecting items by pressing space button.
 
Thank you again.
Sergey Alexandrovich Kryukov at 3-May-13 11:31am
   
Please don't feel like an idiot, we all do mistakes. (But many, many of our inquirers these days look like complete morons, so any of us looks like a genius in contrast, which is not a big honor :-)
 
You just need to expand selection on space button. Then you should handle the KeyPress event to handle space button: look at existing selection and add to it accordingly. Don't touch selection events. Will it resolve the problem?
 
Good luck, call again.
—SA
Wajrak_ at 3-May-13 13:14pm
   
Well I feel like an idiot because I have not thought about making selections from selections Event (I'm still embarrassed).
You solved my problem in your first reply, as soon as you told me what I did I was back on the right path.
I hope I don't make a hobbit out of this "ask a question" because I like to consider my self as a resourceful guy, but it is a second time when you solved my problem in mater of minutes ;)
 
But you make me wonder with one thing you wrote and if you don't mind (and I think you don't, because last time you spent a lot of your time after answering my question explaining some other things to me), it was:
"never, never use such names, rename auto-generated names to something semantic"
This is the first time when someone suggested this to me so I'm quite surprised. If you could let me know why? It would be my pleasure.
Again, Thank you for your time.
Sergey Alexandrovich Kryukov at 3-May-13 13:56pm
   
Well, your constructive self-criticism is one very important key to success :-)
 
My time is quite limited, it's just I allow myself to spend it writing on important issues, in case I feel that it really can help. I this, case, this is easy enough, so I'll explain...
 
—SA
Sergey Alexandrovich Kryukov at 3-May-13 14:18pm
   
I answered in detail. Hope you like it. :-)
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Wajrak wrote:
But you make me wonder with one thing you wrote and if you don't mind (and I think you don't, because last time you spent a lot of your time after answering my question explaining some other things to me), it was: "never, never use such names, rename auto-generated names to something semantic" This is the first time when someone suggested this to me so I'm quite surprised. If you could let me know why? It would be my pleasure. Again, Thank you for your time.

 
Check out Microsoft naming conventions, they are pretty good. Underscore ('_') is not allowed, as well as numerals ("label1", "textBox2"). Isn't it logical? By the way, check out FxCop; it won't justify violation of naming conventions; and this tool is very useful:
http://en.wikipedia.org/wiki/FxCop[^],
http://code.msdn.microsoft.com/codeanalysis[^],
http://msdn2.microsoft.com/en-us/library/bb429476.aspx[^].Also, I noticed extreme idiocy (this time, real idiocy), when in real-life application people use the string "class" in the name of class, "app" or "application" in the name of *.EXE file, and so one, so "label1" for a label is stupid enough. So, why auto-generated code violates Microsoft naming conventions?
 
I think the answer is obvious. The Designer simply cannot do better, is it cannot "know" the semantics. Consider auto-generated names as an approximation #0 and always rename auto-generated names to something semantically sensitive.
 
However, the root of the problem is somewhat deeper. People usually heavily abuse the Designer and almost never under-use it. Do you see how ineffective would be the design when a number of similar labels, buttons and text boxes are put on the form using the Designer? This is a poor manual are repetitive work. No reuse at all. No supportability. Yes, many people act under the impression that the Designer "automates" their work. Why? I think this is apparent:
The Force has a strong influence on the weak minded.
This is the problem.
 
So, the really effective UI design should always be done in code. Similar controls can be put in some array or any collection, so they can be processed in a uniform manner, in loops, and the names — importantly! — can be put into *.resx resource, so the UI will be globalized. No absolute positioning should be used, everything should be docked, so the combination of Dock and Padding properties should make the design, which is also globalized, because it does not make any strong assumption on the sizes of the UI strings. And, one important items is (but many people told me they disagree): in most cases, Designer's manner of adding event handler is no good. It is based on old syntax. It's much better to do using anonymous methods.
 
[EDIT]
 
Please see my past answers where I advocate anonymous methods for event handlers:
how to call keydown event on particular button click[^],
[Solved] How to add Event for C# Control[^].
 
See also some other answers on events:
A question about usercontrols, nested controls and encapsulation.[^],
WPF : How to Use Event in Custom Control[^],
Since we have multicast delegates, why do we need events?[^],
c# networking windows form[^],
Calling an Event as a Function in C++ CLI[^].
 
[END EDIT]
 
In practice, here is what I do it the UI is going to be complex enough: I add main menu and StatusBar, and then I add a hierarchy of panels, docked in each other, is some rare cases, with Splitter. Then I give them all semantic name. The Visual Studio refactorization engine makes it quite easy. Some times, I add something else, not much. And then, I add everything else in code. In code, I add uniform Padding, according to some algorithm. To change the look, I need to change only one or two explicitly defined constants. (And what the user of the Designer-based approach would do? apparently, revisiting all controls, manually!) And, finally, I add all event handlers using "+=". In code. My life is easy, but those who do it all in the Designer suffer a lot. Why? "Obi Wan taught you well."
 
Good luck,
—SA
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100