Delphi 如何从确定的点向ListView的第三列添加数据?
我的目标是将信息、引用添加到每个文件(仅文件)的大小,这些文件必须位于Delphi 如何从确定的点向ListView的第三列添加数据?,delphi,tlistview,Delphi,Tlistview,我的目标是将信息、引用添加到每个文件(仅文件)的大小,这些文件必须位于ListView的第三列 我下面的尝试没有成功。哪里错了 type TForm1 = class(TForm) btn1: TButton; LV1: TListView; procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations }
ListView
的第三列
我下面的尝试没有成功。哪里错了
type
TForm1 = class(TForm)
btn1: TButton;
LV1: TListView;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ListSize: TStrings;
implementation
{$R *.dfm}
function ListFolders(Directory: String): string;
var
FileName, Dirlist: string;
SearchRec: TWin32FindData;
FindHandle: THandle;
ReturnStr: string;
begin
ReturnStr := '';
try
FindHandle := FindFirstFile(PChar(Directory + '*.*'), SearchRec);
if FindHandle <> INVALID_HANDLE_VALUE then
repeat
FileName := SearchRec.cFileName;
if ((SearchRec.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0) then
Dirlist := Dirlist + (FileName + #13);
until FindNextFile(FindHandle, SearchRec) = False;
finally
Winapi.Windows.FindClose(FindHandle);
end;
ReturnStr := (Dirlist);
Result := ReturnStr;
end;
function FileSizeStr(FileName: string): string;
const
// K = Int64(1000); // Comment out this line OR
K = Int64(1024); // Comment out this line
M = K * K;
G = K * M;
T = K * G;
var
size: Int64;
handle: integer;
begin
handle := FileOpen(FileName, fmOpenRead);
if handle = -1 then
Result := 'Unable to open file ' + FileName
else
try
size := FileSeek(handle, Int64(0), 2);
if size < K then
Result := Format('%d bytes', [size])
else if size < M then
Result := Format('%f KB', [size / K])
else if size < G then
Result := Format('%f MB', [size / M])
else if size < T then
Result := Format('%f GB', [size / G])
else
Result := Format('%f TB', [size / T]);
finally
FileClose(handle);
end;
end;
function GetFiles(FileName, Ext: String): String;
Var
SearchFile: TSearchRec;
FindResult: integer;
ListFiles: TStrings;
begin
ListFiles := TStringlist.Create;
ListSize := TStringlist.Create;
FindResult := FindFirst(FileName + Ext, faArchive, SearchFile);
try
While FindResult = 0 do
begin
Application.ProcessMessages;
ListFiles.Add(SearchFile.Name);
ListSize.Add(FileSizeStr(FileName + SearchFile.Name));
FindResult := FindNext(SearchFile);
end;
finally
FindClose(SearchFile)
end;
Result := ListFiles.Text;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
L: TListItem;
vListFolders, vListSize, vListFiles: TStringlist;
i, j, k: integer;
begin
LV1.Clear;
vListFolders := TStringlist.Create;
vListFiles := TStringlist.Create;
vListSize := TStringlist.Create;
vListFolders.Text := ListFolders('C:\');
vListFiles.Text := GetFiles('C:\', '*.*');
vListSize.Text := ListSize.Text;
for i := 0 to vListFolders.Count - 1 do
begin
L := LV1.Items.Add;
L.Caption := vListFolders.Strings[i];
L.SubItems.Add('Folder');
end;
for j := 0 to vListFiles.Count - 1 do
begin
L := LV1.Items.Add;
L.Caption := vListFiles.Strings[j];
L.SubItems.Add('File');
end;
///////////////////////////////////////
for k := 0 to vListSize.Count - 1 do
begin
L := LV1.Items.Add;
L.Caption := vListSize.Strings[k];
// L.SubItems.Add(vListSize.Strings[k]);
end;
///////////////////////////////////////
vListFolders.Free;
vListFiles.Free;
vListSize.Free;
end;
函数
GetFiles(…)
最多只能同时向两个不同的TStringlist添加两个值
function GetFiles(FileName, Ext: String): String;
...
ListFiles.Add(SearchFile.Name);
ListSize.Add(FileSizeStr(FileName + SearchFile.Name));
...
您可以直接将其添加到Listview
var
ListFiles: TStrings;
ListSize : TStrings;
.....
.....
function GetFiles(FileName, Ext: String): String;
Var
SearchFile: TSearchRec;
FindResult: integer;
cCount : Integer;
begin
Result := 'NOT NEEDED';
cCount := LV1.Items.Count -1;
FindResult := FindFirst(FileName + Ext, faArchive, SearchFile);
try
While FindResult = 0 do
begin
inc(cCount);
LV1.Items.Add;
LV1.Items[cCount].Caption := SearchFile.Name;
LV1.Items[cCount].SubItems.Add('File');
LV1.Items[cCount].SubItems.Add(FileSizeStr(FileName + SearchFile.Name));
// C:\ + SearchFile.Name
FindResult := FindNext(SearchFile);
end;
finally
windows.FindClose(SearchFile)
end;
end;
如果必须在过程中执行此操作,请单击(…)
您似乎知道如何添加第二列。对第三列执行完全相同的操作。FWIW,您的代码过于复杂,可以合并。不要在单独的列表/循环中处理每一列。只需执行一个循环,并在运行时添加其子项。不需要为3列创建3个字符串列表。但请稍候,我想我会启动我的IDE并向您演示如何执行此操作。但是,请不要养成期望我们为您重新编写代码的习惯……仔细想想,您的代码中存在的问题远远不止这些。内存泄漏。全局变量。Application.ProcessMessages。很抱歉,您的代码很难以您理解的方式更正。@Jerrydoge,我需要单独处理每一列,因为这些数据将来自我的远程管理应用程序的客户端,并分别请求每个数据。首先是请求文件夹->来文件夹,然后是文件->来文件,最后是以上面显示的相同方式存储在客户端的每个文件的大小。考虑<代码>按钮>代码>我的接收方(服务器端):D。请参阅我的第一个评论的前两个句子。你已经知道怎么做了。重复同样的步骤。请不要指望我们知道这些事情。这里的其他任何人都会在这样的假设下回答:这就是您的代码最终要做的所有事情。它来自单个请求中的服务器这一事实改变了问题的整个范围。你不认为你应该在你的问题中提到这一点,而不是在后面的评论中吗?我选择不回答这个问题,因为它只会继续鼓励极其糟糕的编码实践。这个答案没什么不同。在这里我们可以学到什么呢?还有,PHP是如何出现的?你对这件事有什么其他人都不知道的吗?这样做有什么用呢?我想OP是通过一个脚本从服务器获取数据的。e、 g.
Socket.SendText(“”+(L2.SubItems.Objects[4]…
来自OPEDITION:
在你向一位28天会员抱怨之前,OP到底做错了什么。看看你最初的问题。当你得到帮助时,你有多高兴。Eureka!非常感谢,这给了我一个更好的主意。你不一定要写全部内容,但你做得很好。这相当多h回答了我的问题,非常感谢!
@KKK:我已经删除了PHP部分。感谢您让我知道。2)最好像现在这样发送一个独特的请求。
var
ListFiles: TStrings;
ListSize : TStrings;
.....
.....
function GetFiles(FileName, Ext: String): String;
Var
SearchFile: TSearchRec;
FindResult: integer;
cCount : Integer;
begin
Result := 'NOT NEEDED';
cCount := LV1.Items.Count -1;
FindResult := FindFirst(FileName + Ext, faArchive, SearchFile);
try
While FindResult = 0 do
begin
inc(cCount);
LV1.Items.Add;
LV1.Items[cCount].Caption := SearchFile.Name;
LV1.Items[cCount].SubItems.Add('File');
LV1.Items[cCount].SubItems.Add(FileSizeStr(FileName + SearchFile.Name));
// C:\ + SearchFile.Name
FindResult := FindNext(SearchFile);
end;
finally
windows.FindClose(SearchFile)
end;
end;
var
ListFiles: TStrings;
ListSize : TStrings;
.....
.....
function GetFiles(FileName, Ext: String): String;
Var
SearchFile: TSearchRec;
FindResult: integer;
begin
FindResult := FindFirst(FileName + Ext, faArchive, SearchFile);
try
While FindResult = 0 do
begin
ListFiles.Add(SearchFile.Name);
ListSize.Add(FileSizeStr(FileName + SearchFile.Name));
FindResult := FindNext(SearchFile);
end;
finally
windows.FindClose(SearchFile)
end;
Result := 'NOT NEEDED';
end;
procedure TForm1.btn1Click(Sender: TObject);
var
vListFolders : TStringlist;
i, j, cCount : integer;
begin
ListFiles := TStringlist.Create;
ListSize := TStringlist.Create;
vListFolders := TStringlist.Create;
LV1.Items.Clear;
try
vListFolders.Text := ListFolders('C:\');
GetFiles('C:\', '*.*'); // Add to ListFiles, ListSize Name and size
// Old Only for folders ...
// ========================================
for i := 0 to vListFolders.Count - 1 do
begin
L := LV1.Items.Add;
L.Caption := vListFolders.Strings[i];
L.SubItems.Add('Folder');
end;
// New for name and size
// ========================================
cCount := LV1.Items.Count -1;
if ListFiles.Count = ListSize.Count then
begin
for j := 0 to ListFiles.Count - 1 do
begin
Inc(cCount);
LV1.Items.Add;
LV1.Items[cCount].Caption := ListFiles[j]; // File Name
LV1.Items[cCount].SubItems.Add('File'); // File
LV1.Items[cCount].SubItems.Add(ListSize[j]); // Formatted Size
end; // for j
end else begin
Inc(cCount);
LV1.Items.Add;
LV1.Items[cCount].Caption := 'File List BROKEN';
end;
finally
vListFolders.Free;
ListFiles.Free;
ListSize .Free;
end;
end; // btn1Click(...)