unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids, Buttons, ComCtrls, ExtCtrls;
type
  TForm1 = class(TForm)
    ADOConn: TADOConnection;
    ADOQ: TADOQuery;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Sou: TSpeedButton;
    shan: TSpeedButton;
    xiao: TSpeedButton;
    wei: TSpeedButton;
    zhuan: TSpeedButton;
    Sta: TStatusBar;
    Panel1: TPanel;
    Panel2: TPanel;
    Edit1: TEdit;
    Edit2: TEdit;
    SpeedButton1: TSpeedButton;
    CheckBox1: TCheckBox;
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure xiaoClick(Sender: TObject);
    procedure SouClick(Sender: TObject);
    procedure shanClick(Sender: TObject);
    procedure weiClick(Sender: TObject);
    procedure zhuanClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function sumrecordpage(rpage:integer):integer;
    procedure fenye;  //sql语句分页
    procedure souye; //首页
    procedure buttonzt(a,b,c,d:boolean);//按钮状态
    procedure xiaoye;   //下页
    procedure shanye;    //上页
    procedure weiye;      //尾页
    procedure danqiansta;  //
    procedure tiaoye;  //跳到
  end;
var
  Form1: TForm1;
  tmp,psize,dye,youye:integer;  //psize一页多少记录,dye现在的当前页是多少,youye是总页数,tmp是多少条记录
implementation
{$R *.dfm}
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then key:=#0;
end;
procedure TForm1.fenye;
var
sql:string;
begin
if dye=1 then begin
sql:='select top '+inttostr(psize)+' * from 进药表 where in_id>(select max (in_id) from (select top '+inttostr(psize)+' in_id from 进药表 order by in_id) as tabtmp) order by in_id';
  adoq.Close;
  adoq.SQL.Clear;
   adoq.SQL.Add(sql);
    adoq.Open;
    end else
    begin
    sql:='select top '+inttostr(psize)+' * from 进药表 where in_id>(select max (in_id) from (select top '+inttostr(psize*dye)+' in_id from 进药表 order by in_id) as tabtmp) order by in_id';
  adoq.Close;
  adoq.SQL.Clear;
   adoq.SQL.Add(sql);
    adoq.Open;
    end;
end;
function TForm1.sumrecordpage(rpage: integer): integer;  //rpage 每页多少条记录 返回多少页
var
sumado:tadoquery;
begin
  sumado:=tadoquery.Create(self);
   sumado.Connection:=adoconn;
     with sumado do
      begin
        close;
         sql.Clear;
           sql.Add('select in_id from 进药表');
            open;
          
          tmp:=recordcount;
if tmp=0 then
begin
result:=1;
end else begin
         if (tmp mod rpage)=0 then
           result:=(tmp div rpage) else
           result:= (tmp div rpage)+1;
end;
        close;
       free;
      end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
psize:=10;
dye:=0;
youye:=sumrecordpage(psize);//有多少页
souye;
sta.Panels[0].Text:=' 共有'+inttostr(youye)+'页 '+inttostr(tmp)+'条记录';
end;
procedure TForm1.souye;
begin
if dye=0 then
   begin
     adoq.Close;
         adoq.SQL.Clear;
       adoq.SQL.Add('select top '+inttostr(psize)+' * from 进药表 order by in_id');
     adoq.Open;
     if adoq.RecordCount<psize then buttonzt(false,false,false,false) else buttonzt(false,false,true,true);
    end else
    begin
    fenye;
    end;
    danqiansta;
end;
procedure TForm1.buttonzt(a, b, c, d: boolean);
begin
sou.Enabled:=a;
shan.Enabled:=b;
xiao.Enabled:=c;
wei.Enabled:=d;
end;
procedure TForm1.xiaoye;
begin
inc(dye);
fenye;
if dye=youye-1 then buttonzt(true,true,false,false) else buttonzt(true,true,true,true);
end;
procedure TForm1.xiaoClick(Sender: TObject);
begin
xiaoye;
danqiansta;
end;
procedure TForm1.SouClick(Sender: TObject);
begin
dye:=0;
souye;
end;
procedure TForm1.shanye;
begin
dec(dye);
if dye=0 then souye else fenye;
if not (dye=0) then buttonzt(true,true,true,true);
end;
procedure TForm1.shanClick(Sender: TObject);
begin
shanye;
danqiansta;
end;
procedure TForm1.weiye;
begin
dye:=youye-1;
fenye;
buttonzt(true,true,false,false);
end;
procedure TForm1.weiClick(Sender: TObject);
begin
weiye;
danqiansta;
end;
procedure TForm1.danqiansta;
begin
sta.Panels[1].Text:=' 当前是第'+inttostr(dye+1)+'页';
end;
procedure TForm1.tiaoye;
var
tstr:integer;
begin
    if edit1.Text='' then
    begin
     messagedlg('页数不能为空',mtWarning,[mbOK],0);
     exit;
    end;
tstr:=strtoint(trim(edit1.Text));
   if tstr>youye then
     begin
     messagedlg('输入的页数不能大于总页数',mtWarning,[mbOK],0);
     edit1.Text:='';
     edit1.SetFocus;
     exit;
      end;
  dye:=tstr-1;
if tstr=1 then
begin
dye:=0;
souye;
danqiansta;
end else begin
fenye;
danqiansta;
if dye=youye-1 then buttonzt(true,true,false,false) else buttonzt(true,true,true,true);
end;

end;
procedure TForm1.zhuanClick(Sender: TObject);
begin
tiaoye;
end;
end.