Delphi:根据文件名称在 TreeView 中创建文件树

标签 delphi treeview

我有一个文件字符串列表和一个日期作为其名称(分隔符可以不同:“-|.”;掩码:yyyy/mm/dd):

2011-03-12.jpeg
2011|10-15.doc
2011.08-09.rar
2011.10-15.txt
2011-03-14.jpeg
2011.06.23.mp3
2011|07|01.zip
2011-07-05.rar

如何使用它们创建 TreeView ?所有文件必须按月份和日期排序+分配到月份部分,例如:

enter image description here

非常感谢您的帮助!!!

最佳答案

由于您已经填充了 TStringList,我只需使用其 CustomSort() 方法对其进行排序,然后您可以循环遍历它,将节点添加到树中根据需要,例如:

function SortFilesByMonthAndDay(List: TStringList; Index1, Index2: Integer): Integer;
var
  Value1, Value2: Integer;
begin
  Value1 := StrToInt(Copy(List[Index1], 6, 2));
  Value2 := StrToInt(Copy(List[Index2], 6, 2));
  if Value1 = Value2 then
  begin
    Value1 := StrToInt(Copy(List[Index1], 9, 2));
    Value2 := StrToInt(Copy(List[Index2], 9, 2));
  end;
  Result := Value2 - Value1;
end;

var
  I: Integer;
  FileMonth, CurrentMonth: Integer;
  CurrentMonthNode: TTreeNode;
begin
  CurrentMonth := 0;
  CurrentMonthNode := nil;
  Files.CustomSort(@SortFilesByMonthAndDay);
  for I := 0 to Files.Count-1 do
  begin
    FileMonth := StrToInt(Copy(Files[I], 6, 2));
    if FileMonth <> CurrentMonth then
    begin
      CurrentMonth := FileMonth;
      CurrentMonthNode := TreeView1.Items.Add(nil, SysUtils.LongMonthNames[CurrentMonth]);
    end;
    TreeView1.Items.AddChild(CurrentMonthNode, Files[I]);
  end;
end;

关于Delphi:根据文件名称在 TreeView 中创建文件树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8660546/

相关文章:

delphi - 是否可以在 TDateEdit Firemonkey 控件上指定最短日期?

windows - 如何使文件的创建、访问和修改日期与 Windows 属性相同?

delphi - 如何使 TTreeView 在禁用时不突出显示所有节点?

WPF TreeView 分层绑定(bind)。

delphi - 是否可以在运行时修改 VCL 样式?

delphi - 为什么Delphi2009+中的SizeOf(Char) = 2?

delphi - 在德尔福插入排序算法中找不到简单的错误

c# - 移除或添加节点时 TreeView 的事件

wpf - 仅当树项的父项是列表中的第一个时,WPF 将命令标记为不可用的方法是什么?

c# - 在派生的 C# 用户控件中处理 Windows 通知