How to replace a TDBNavigator with a TSpeedButton?

220 views Asked by At

I did:

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  DataTable.qOrders.Next;
end;

It works, but the problem is when I click the button to reach the last record, the button is not disabled, like in a TDBNavigator.

How did I make the TSpeedButton disable and enable automatically like the TDBNavigator does?

2

There are 2 answers

0
Uwe Raabe On

Drop a TActionList onto your form and add the standard dataset actions to it. Connect these actions to your dataset and your speedbuttons to the appropriate actions. These standard actions will handle the enable state according to the current dataset state.

0
Zhorov On

Here is a simple solution, that works perfectly for me.

I have a form (frmMain), dataset (dsWork), datasource (srcWork), grid and two speedbuttons (btnNext and btnPrior). The important part is in "OnDataChange" event of TDataSource. Here is the source code:

unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, DBTables, StdCtrls, ExtCtrls;

type
  TfrmMain = class(TForm)
    btnNext: TButton;
    srcWork: TDataSource;
    dsWork: TTable;
    btnPrior: TButton;
    grdWork: TDBGrid;
    procedure btnNextClick(Sender: TObject);
    procedure btnPriorClick(Sender: TObject);
    procedure srcWorkDataChange(Sender: TObject; Field: TField);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.btnNextClick(Sender: TObject);
begin
   if not dsWork.Eof then dsWork.Next;
end;

procedure TfrmMain.btnPriorClick(Sender: TObject);
begin
   if not dsWork.Bof then dsWork.Prior;
end;

procedure TfrmMain.srcWorkDataChange(Sender: TObject; Field: TField);
begin
   btnNext.Enabled := not dsWork.Eof;
   btnPrior.Enabled := not dsWork.Bof;
end;

end.