简体   繁体   English

除了上次修改的文件之外,如何检查每个ListView组中的所有文件? C#

[英]How to Check All Files in Each ListView Group Except Last Modified File? C#

How to check checkboxes of all files in each ListView Group except the Last Modified File? 如何检查每个ListView组中除最后修改文件以外的所有文件的复选框?

I've programmatically created a list of 500 files in multi-column ListView1 in C#: 我以编程方式在C#中的多列ListView1中创建了500个文件的列表:

File Name , Author , Last Modified Date (Total 3 Columns)

Group 1: (All files with John as Author)
--------
File1.txt, John, 17-01-2019 8:09:21 PM
File4.jpg, John, 20-01-2019 5:04:21 PM
File6.docx, John,25-12-2014 3:02:40 AM

Group 2: (All files with Muller as Author)
--------
File2.txt, Muller, 13-07-2013 2:02:21 AM
File3.jpg, Muller, 10-04-2012 4:04:21 AM
File5.docx, Muller, 15-10-2016 8:04:40 PM
File7.png, Muller, 20-03-2019 4:15:20 PM
File8.xml, Muller, 10-06-2015 8:40:00 AM

Group 3:
...............
and the list goes on...

I want to programmatically check-up checkboxes of all files in each group except the Last Modified File. 我想以编程方式检查每个组中除最后修改文件之外的所有文件的复选框。 I tried for several hours with different codes with no luck. 我用不同的代码试了好几个小时没有运气。

Your help is really appreciated.. 非常感谢您的帮助..

foreach (ListViewGroup grp in listFiles.Groups)
    {
      bool FirstItem = false;
        foreach (ListViewItem item in grp.Items)
        {
            if (!FirstItem)
            {
                item.Checked = false;
                FirstItem = true;
            }
            else
            {
                item.Checked = true;
            }
        }
    }

Keep two variables, last and curr, initially null. 保留两个变量,last和curr,最初为null。 For each item, set curr = item, if curr's last modified date is later than prev's last modified date, or if last is null, set last = curr. 对于每个项目,设置curr = item,如果curr的上次修改日期晚于prev的上次修改日期,或者last为null,则设置last = curr。 At the end of this loop, last is the item which has the latest last modified date. 在此循环结束时,last是具有最新上次修改日期的项目。 Do another loop and check everything except last. 做另一个循环,并检查除了最后一切。

You could do this in one loop, but I'll leave it as an exercise. 你可以在一个循环中完成这个,但我会把它留作练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM