【问题标题】:Progress Bar progression while saving a StringGrid保存 StringGrid 时的进度条进度
【发布时间】:2013-05-25 03:08:52
【问题描述】:

我正在开发一个程序,上面有一个 StringGrid;当按下特定按钮时,我的程序将 stringgtid 保存到 c:\myfolder\tab9.txt。我想放置一个进度条,指示保存过程结束时还剩多少时间,因为有时网格有很多行,可能需要一些时间。我正在使用此代码:

procedure SaveSG(StringGrid:TStringGrid; const FileName:TFileName);
var
  f:    TextFile;
  i,k: Integer;
begin
  AssignFile(f, FileName);
  Rewrite(f);
  with StringGrid do
  begin
    Writeln(f, ColCount); // Write number of Columns
    Writeln(f, RowCount); // Write number of Rows
    for i := 0 to ColCount - 1 do  // loop through cells of the StringGrid
      for k := 0 to RowCount - 1 do
         Writeln(F, Cells[i, k]);
        end;
  CloseFile(F);
end; 

我这样调用程序:SaveSG(StringGrid1,'c:\myfolder\myfile.txt');。我的问题是我不明白如何做一个指示保存进度的进度条。目前我只声明了ProgressBar1.Position:=0ProgressBar1.Max:=FileSize。你有什么建议吗?

【问题讨论】:

  • 如果你想正确地这样做,你应该在它自己的线程中加载文件,然后定期向进度条发送消息。

标签: delphi


【解决方案1】:

我们在谈论多少个细胞?您的主要瓶颈是您正在为每个单元格写入文件,而不是进行缓冲写入。

我建议你用来自 TStringGrid 的数据填充 TStringList,并使用 TStringList.SaveToFile() 方法。

我已经在具有 10,000,000 个单元格(10,000 行 x 1,000 列)的 StringGrid 上测试了以下过程,它可以在不到一秒的时间内将数据保存到磁盘:

procedure SaveStringGrid(const AStringGrid: TStringGrid; const AFilename: TFileName);
var
  sl    : TStringList;
  C1, C2: Integer;
begin
  sl := TStringList.Create;
  try
    sl.Add(IntToStr(AStringGrid.ColCount));
    sl.Add(IntToStr(AStringGrid.RowCount));
    for C1 := 0 to AStringGrid.ColCount - 1 do
      for C2 := 0 to AStringGrid.RowCount - 1 do
        sl.Add(AStringGrid.Cells[C1, C2]);
    sl.SaveToFile(AFilename);
  finally
    sl.Free;
  end;
end;

【讨论】:

  • 谢谢,这很有用。
猜你喜欢
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 2016-11-28
相关资源
最近更新 更多