文件文件内容如下所示:
代码 名称
110000,北京市
110100,北京市市辖区
110101,北京市东城区
110102,北京市西城区
110103,北京市崇文区
.... , ........
共有五千多行.读出之后将代码和名称部分 分开,存放在StringGrid(或其它类型控件上)让其一个代码对应一个名称,然后每页显示20条数据,点击下一页显示后面的数据.并能提供首页,第一页,最后一页,末页,转到某一页的功能.另外如此多的数据行数能否做优化处理.请各位大侠多多指教了!谢谢!
代码 名称
110000,北京市
110100,北京市市辖区
110101,北京市东城区
110102,北京市西城区
110103,北京市崇文区
.... , ........
共有五千多行.读出之后将代码和名称部分 分开,存放在StringGrid(或其它类型控件上)让其一个代码对应一个名称,然后每页显示20条数据,点击下一页显示后面的数据.并能提供首页,第一页,最后一页,末页,转到某一页的功能.另外如此多的数据行数能否做优化处理.请各位大侠多多指教了!谢谢!
解决方案 »
- TstringList奇怪错误,地址错误?
- dbgrid 的操作问题
- ado增強控件有沒有下載?
- 我的本意是用户名和口令不区分大小写 以上的SQL出错,是否Access不支持Upper函数?如何处理?
- 关于socket与临界区的问题
- 很急手的问题,在线等待(2小时后散分)!
- delphi控制向excel末尾插入一条记录
- 写了一个组件,其中的方法耗时大,且方法中有不能使用Application.ProcessMessages语句,有何解?如果我要加入进度条,如何加?
- 如何删除我的HotMail帐号,在线等候,非常急。如果成功立即给分。
- 使用Delphi5.x开发ASP组件读取网页源码出错:必须加载一个dll
- 求助:Txflowchart 控件怎样加滚动条?
- DBGrid.columns下的title中的caption 修改中文名字执行无效果保存后不纪录!
//五千行应该没问题吧?试试,试试 ^_^procedure TForm1.Button1Click(Sender: TObject);
var
SS, SS2: TStringList;
I, L : integer;
begin
SS := TStringList.Create;
SS2 := TStringList.Create;
SS.LoadFromFile('c:\test.txt');
L := 1;
for I := 0 to SS.Count-1 do begin
SS2.Text := StringReplace(SS[I], ',', #10, [rfReplaceAll]);
StringGrid1.Cells[1,L+I] := SS2[0];
StringGrid1.Cells[2,L+I] := SS2[1];
StringGrid1.RowCount := StringGrid1.RowCount+1;
end;
SS2.Free;
SS.Free;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function GridPageScroll(Arrow: boolean): boolean;
end;var
Form1: TForm1;implementation{$R *.dfm}var
SS: TStringList;
Page: integer = 0;//当前页码
PerPage: integer = 4;//每页行数 我用每页4行测试的。你自己改吧function TForm1.GridPageScroll(Arrow: boolean): boolean;
var
SS2: TStringList;
I, F, T, L : integer;
begin
Result := True;
if Arrow then Inc(Page) else Dec(Page);
if (Page<1) or (Page>(SS.Count div PerPage + 1)) then begin
Result := False;
Exit;
end else
SS2 := TStringList.Create;
F := Page * PerPage - PerPage;
T := Page * PerPage;
if Page * PerPage > SS.Count-1 then T := SS.Count-1;
L := 1;
for I := F to T do begin
SS2.Text := StringReplace(SS[I], ',', #10, [rfReplaceAll]);
StringGrid1.Cells[1,L] := SS2[0];
StringGrid1.Cells[2,L] := SS2[1];
Inc(L);
end;
SS2.Free;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if not GridPageScroll(True) then Showmessage('已到尾'); //向后翻
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if not GridPageScroll(False) then Showmessage('已到头'); //向前翻
end;procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.RowCount := PerPage+1;
SS := TStringList.Create;
SS.LoadFromFile('c:\test.txt');
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if SS<>nil then FreeAndNil(SS);
end;
end.
朋友啊,上面实现的是向后翻页和向前翻页。至于第一页、末页、转到某一页,你自己改程序吧!其实就是利用了Page这个当前页变量!不要企望别人帮你写一个完完整整的程序啊.....^_^
if SS[I]='10010,北京市' then
Found!I就是行号!通过I与每页数量,确定出它在哪一页,然后“翻页”到那页显示就行了道理很简单,代码写起来很费力,因为要考虑各种错误产生的因素
{实现上翻、下翻、到某页、首页、末页}
{还是那句话,有点小问题,是细节问题,自己改}unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Button4: TButton;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function GridPageScroll: boolean;
end;var
Form1: TForm1;implementation{$R *.dfm}var
SS: TStringList;
Page: integer = 1;//当前页码
PerPage: integer = 4;//每页行数function TForm1.GridPageScroll: boolean;
var
SS2: TStringList;
I, F, T, L : integer;
begin
Result := True;
if (Page<1) or (Page>(SS.Count div PerPage)+1) then begin
Result := False;
Exit;
end;
SS2 := TStringList.Create;
F := Page * PerPage - PerPage;
T := Page * PerPage;
if Page * PerPage > SS.Count-1 then T := SS.Count-1;
L := 1;
for I := F to T do begin
SS2.Text := StringReplace(SS[I], ',', #10, [rfReplaceAll]);
StringGrid1.Cells[1,L] := SS2[0];
StringGrid1.Cells[2,L] := SS2[1];
Inc(L);
end;
SS2.Free;
Edit1.Text := InttoStr(Page);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if not GridPageScroll then begin
Showmessage('已到尾');
Dec(Page);
end else Inc(Page);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if not GridPageScroll then begin
Showmessage('已到头');
Inc(Page);
end else Dec(Page);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Page := StrtoInt(Edit1.Text);
GridPageScroll;
end;procedure TForm1.Button4Click(Sender: TObject);
begin
Page := 1;
GridPageScroll;
end;procedure TForm1.Button5Click(Sender: TObject);
begin
Page := SS.Count div PerPage + 1;//这句有问题,自己改
GridPageScroll;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := '向下翻';
Button2.Caption := '向上翻';
Button3.Caption := '跳到某页';
Button4.Caption := '首页';
Button5.Caption := '末页';
Edit1.Text := '1';
StringGrid1.RowCount := PerPage+1;
SS := TStringList.Create;
SS.LoadFromFile('c:\test.txt');
GridPageScroll;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if SS<>nil then FreeAndNil(SS);
end;end.