【发布时间】:2018-06-26 06:36:06
【问题描述】:
有没有办法在一个字段上对列表框进行排序?
(我看过其他帖子,但我认为这涉及更多)。我可以做到这一点,但认为有一个更快的速记版本我只是不知道。
基本上这会读取其中所有文件夹的目录,格式为:
DATENAME
我从日期中解析出名称。我需要按名称组织这些然后按日期(第二个过滤器是什么让我绊倒)。
所以一个文件夹:
12012016TULLY
1202019LAVA
2202018LAVA
5162019CLOUD
5202020LAVA
看起来像
5162019CLOUD
2202018LAVA
1202019LAVA
5202020LAVA
12012016TULLY
这就是我所拥有的:
class MyListBoxItem
{
public string StudyBaseFolder { get; set; }
public string StudyName { get; set; }
public string UserLastName { get; set; }
public string StudyDate { get; set; }
}
List<MyListBoxItem> studiesAndFolders = new List<MyListBoxItem>();
//later int he code i build a list of studyNames (which is a path and I pasre the path here too)
foreach (string sn in studyName)
{
//get user name
String lastName = getLastName(sn);
String theDate = getDate(sn);
//can I organize this based on the LAST NAME THEN THE DATE
studiesAndFolders.Add(new MyListBoxItem { StudyBaseFolder = path, StudyName = sn, UserLastName = lastName, StudyDate = theDate });
}
然后我终于将它添加到列表框中。
listDirectories.Items.Clear();
//I do it this way so a double click on an item gets the object back and I can do things with it.
foreach(MyListBoxItem direc in studiesAndFolders)
{
listDirectories.Items.Add(direc);
}
listbox.sorted=true 没有帮助,我确信可能有一个表达式(LINQ 来救援?)。当我把studiesAndFolders 放到列表中时,我正打算用大量的案例来做这件事。
【问题讨论】:
-
你使用的是什么 UI 框架?
-
@Freggar Visual Studio 和标准 Windows UI(忘记它叫什么了。Windows 窗体?桌面应用程序)
-
为什么不使用 LINQ?
-
为什么不在添加到
ListBox之前简单地进行排序?