计算ListView项,SelectedItems返回错误的值 [英] Counting ListView items, SelectedItems returns wrong value

查看:94
本文介绍了计算ListView项,SelectedItems返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello good people,



我需要有人告诉我,我没有失去理智,为什么?因为我要问的是愚蠢的。好的,我们走了:



我有一个ListView,其中填充了文件名。我希望它使用多个选择,但就像在Windows Commander中一样,只需使用向上/向下箭头键并通过简单地按空格键来选择行。我已经创建了Key Up事件捕获空间按钮,即使我正在获取当前所选(突出显示)行的索引并将其添加到我的List< int>。一切都像魅力一样。



我为我的ListView,ItemSelectionChanged创建了另一个事件,在这个事件中我有一个小循环:



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:

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;
        }
    }
}





所有的桃子,但无论何时我都是尝试使用listViewLeft.SelectedItems.Count它随机返回0或1。最好的事情是,如果我使用ctrl和鼠标选择行,它就像一个魅力。

我缺少什么?什么区别listViewLeft.Items [selectedIndex] .Selected = true并使用鼠标选择行?



不要误解我只需计算我的List< int>中的项目就可以解决这个问题。但它只是让我很烦,因为我从未遇到过这样的问题。



感谢您的时间,如果我复杂的话,我会道歉;)

Wajrak



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

推荐答案

您的方法名称表明您在更改选择时处理事件(永远不要使用此类名称,将自动生成的名称重命名为语义;并且永远不要问这样的问题;始终显示如何将处理程序添加到事件实例的调用列表+ =运算符)。现在看看你在做什么: 你正在改变选择改变方法中的选择 !这可能导致相当复杂的行为,但不是随机的。你只是没有得到那个。



我不知道你想要达到什么,但就是不要这样做。做点别的。 :-)



-SA
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. :-)

—SA


Wajrak写道:

但你让我想知道你写的一件事,如果你不介意(我想你不会,因为你上次花了很多时间)在回答我的问题向我解释其他一些事情之后),它是:永远,永远不要使用这样的名字,将自动生成的名称重命名为语义这是有人第一次向我建议这样,所以我很惊讶。如果你能让我知道为什么?这是我的荣幸。再次感谢您的时间。

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.





查看Microsoft命名约定,它们非常好。不允许使用下划线(''_''),以及数字(label1,textBox2)。这不合逻辑吗?顺便说一句,看看FxCop;它不能成为违反命名惯例的理由;这个工具非常有用:

http://en.wikipedia.org/wiki/FxCop [ ^ ],

< a href =http://code.msdn.microsoft.com/codeanalysis> http://code.msdn.microsoft.com/codeanalysis [ ^ ],

http://msdn2.microsoft.com/en-us/library/bb429476.aspx [ ^ ]。另外,我注意到极端白痴(这一次,真正的白痴),当在现实生活中应用时人们使用类名称中的字符串class,* .EXE文件名称中的app或application等等,因此标签的label1足够愚蠢。那么,为什么自动生成的代码违反了Microsoft的命名约定?



我认为答案是显而易见的。设计师根本无法做得更好,是不是知道语义。将自动生成的名称视为近似值#0,并始终将自动生成的名称重命名为语义敏感的内容。



然而,问题的根源更深一些。人们通常会严重滥用设计师,几乎从不使用它。您是否看到使用Designer在表单上放置了许多类似的标签,按钮和文本框时,设计效果如何?这是一本糟糕的手册,是重复性的工作。完全没有重用。没有可支持性。是的,很多人都认为设计师自动化他们的工作。为什么?我认为这很明显:



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.

这就是问题所在。



因此,真正有效的UI设计应始终在代码中完成。类似的控件可以放在一些数组或任何集合中,因此它们可以以统一的方式,循环和名称进行处理 - 重要的是! - 可以放入* .resx资源,因此UI将全球化。不应该使用绝对定位,所有内容都应该停靠,因此 Dock Padding 属性的组合应该进行设计,这也是全球化的,因为它没有对UI字符串的大小做出任何强有力的假设。并且,一个重要的项目是(但很多人告诉我他们不同意):在大多数情况下,Designer的添加事件处理程序的方式并不好。它基于旧语法。使用匿名方法要好得多。







请参阅我过去的答案,我提倡事件处理程序的匿名方法:

如何在特定按钮点击时调用keydown事件 [ ^ ],

[已解决]如何为C#Control添加事件 [< a href =http://www.codeproject.com/Answers/187225/Solved-How-to-add-Event-for-Csharp-Control.aspx\"target =_ blanktitle =New Window> ^ ]。



另见事件的其他答案:

关于用户控件,嵌套控件和封装的问题。 [ ^ ],

WPF:如何在自定义控件中使用事件 [ ^ ],

因为我们有多播代表,为什么我们需要活动? [ ^ ],

c#网络窗体 [ ^ ],

在C ++ CLI中将事件作为函数调用 [ ^ ]。



[END EDIT ]



在实践中,这就是我的工作,UI将变得足够复杂:我添加主菜单和 StatusBar ,然后我添加一个面板层次结构,彼此停靠,是一些罕见的情况, Splitter 。然后我给他们所有的语义名称。 Visual Studio重构引擎使其变得非常简单。有时候,我添加其他东西,不多。然后,我在代码中添加其他所有内容。在代码中,我根据一些算法添加了统一的 Padding 。要改变外观,我只需要更改一个或两个显式定义的常量。 (基于Designer的方法的用户会做什么?显然,手动重新访问所有控件!)最后,我使用+ =添加所有事件处理程序。在代码中。我的生活很轻松,但是那些在设计师中做到这一切的人会受到很多苦难。为什么? 欧比万教你很好。



祝你好运,

-SA

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.



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


这篇关于计算ListView项,SelectedItems返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆