unit U_PositionSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, DB, ADODB, U_BaseHelp, System.ImageList, Vcl.ImgList,
  Datasnap.DBClient, Vcl.StdCtrls, math;

type
  TfrmPositionSel = class(TfrmBaseHelp)
    ScrollBox1: TScrollBox;
    btn1: TSpeedButton;
    btn2: TSpeedButton;
    btn3: TSpeedButton;
    btn4: TSpeedButton;
    btn5: TSpeedButton;
    btn6: TSpeedButton;
    btn7: TSpeedButton;
    btn8: TSpeedButton;
    btn58: TSpeedButton;
    btn9: TSpeedButton;
    btn10: TSpeedButton;
    btn11: TSpeedButton;
    btn12: TSpeedButton;
    btn13: TSpeedButton;
    btn14: TSpeedButton;
    btn15: TSpeedButton;
    btn59: TSpeedButton;
    btn16: TSpeedButton;
    btn17: TSpeedButton;
    btn18: TSpeedButton;
    btn19: TSpeedButton;
    btn20: TSpeedButton;
    btn21: TSpeedButton;
    btn22: TSpeedButton;
    btn60: TSpeedButton;
    btn23: TSpeedButton;
    btn24: TSpeedButton;
    btn25: TSpeedButton;
    btn26: TSpeedButton;
    btn27: TSpeedButton;
    btn28: TSpeedButton;
    btn29: TSpeedButton;
    btn61: TSpeedButton;
    btn30: TSpeedButton;
    btn31: TSpeedButton;
    btn32: TSpeedButton;
    btn33: TSpeedButton;
    btn34: TSpeedButton;
    btn35: TSpeedButton;
    btn36: TSpeedButton;
    btn62: TSpeedButton;
    btn37: TSpeedButton;
    btn38: TSpeedButton;
    btn39: TSpeedButton;
    btn40: TSpeedButton;
    btn41: TSpeedButton;
    btn42: TSpeedButton;
    btn43: TSpeedButton;
    btn63: TSpeedButton;
    btn44: TSpeedButton;
    btn45: TSpeedButton;
    btn46: TSpeedButton;
    btn47: TSpeedButton;
    btn48: TSpeedButton;
    btn49: TSpeedButton;
    btn50: TSpeedButton;
    btn64: TSpeedButton;
    btn51: TSpeedButton;
    btn52: TSpeedButton;
    btn53: TSpeedButton;
    btn54: TSpeedButton;
    btn55: TSpeedButton;
    btn56: TSpeedButton;
    btn57: TSpeedButton;
    ADOTmp: TADOQuery;
    ADOConnection1: TADOConnection;
    CDS_All: TClientDataSet;
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FPOSNO: string;
    FPage: Integer;
    { Public declarations }
  end;

var
  frmPositionSel: TfrmPositionSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmPositionSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmPositionSel := nil;
end;

procedure TfrmPositionSel.InitGrid();
var
  i: Integer;
  str: string;
begin

  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select POSNO,POSName      ');
    sql.Add(',ROW_NUMBER() over(order by A.POSNO) as keyNo  ');
    sql.Add('from Bs_Position  A  ');
    SQL.Add('order by POSNO,POSName ');
    Open;
  end;
  if ADOTmp.IsEmpty then
  begin
    Application.MessageBox('没有定义数据！', '提示', 0);
    Exit;
  end;
  SCreateCDS(ADOTmp, CDS_All);
  SInitCDSData(ADOTmp, CDS_All);

  if CDS_All.RecordCount < FPage * 64 then
  begin
    FPage := FPage - 1;
  end;
  if FPage <= 0 then
  begin
    FPage := 1;
  end;
  if CDS_All.RecordCount mod 64 > 0 then
    Label1.Caption := IntToStr(FPage) + '/' + IntToStr(Floor(CDS_All.RecordCount / 64 + 1))
  else
    Label1.Caption := IntToStr(FPage) + '/' + IntToStr(Floor(CDS_All.RecordCount / 64));

  for i := 0 to 63 do
  begin
    if CDS_All.Locate('keyNo', (FPage - 1) * 64 + i + 1, []) then
    begin
      with ScrollBox1 do
      begin
        TSpeedButton(Controls[i]).Visible := True;
        TSpeedButton(Controls[i]).Hint := CDS_All.fieldbyname('POSNO').AsString;
        TSpeedButton(Controls[i]).Caption := CDS_All.fieldbyname('POSNO').AsString;
      end;
    end
    else
    begin
      with ScrollBox1 do
      begin
        TSpeedButton(Controls[i]).Visible := False;
        TSpeedButton(Controls[i]).Hint := '';
        TSpeedButton(Controls[i]).Caption := '';
      end;
    end;
  end;
end;

procedure TfrmPositionSel.FormShow(Sender: TObject);
begin
  inherited;
  FPage := 1;
  InitGrid();
end;

procedure TfrmPositionSel.btn1Click(Sender: TObject);
begin
  FPOSNO := Trim(TSpeedButton(Sender).Hint);
  ModalResult := 1;
end;

procedure TfrmPositionSel.Button1Click(Sender: TObject);
begin
  FPage := FPage - 1;
  InitGrid();
end;

procedure TfrmPositionSel.Button2Click(Sender: TObject);
begin
  FPage := FPage + 1;
  InitGrid();
end;

procedure TfrmPositionSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
 // Action:=caHide;

end;

procedure TfrmPositionSel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    {if Application.MessageBox('网络连接失败,是否要再次连接?','提示',32+4)=IDYES then
    begin
      try
        with ADOConnection1 do
        begin
          Connected:=false;
          ConnectionString:=DConString;
          //ConnectionString:='23242';
          Connected:=true;
        end;
      except
      end;
    end; }
  end;
end;

end.

