1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| unit liketest;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
FireDAC.Stan.Intf,
FireDAC.Stan.Option,
FireDAC.Stan.Error,
FireDAC.UI.Intf,
FireDAC.Phys.Intf,
FireDAC.Stan.Def,
FireDAC.Stan.Pool,
FireDAC.Stan.Async,
FireDAC.Phys,
FireDAC.Phys.MySQL,
FireDAC.Phys.MySQLDef,
FireDAC.VCLUI.Wait,
FireDAC.Stan.Param,
FireDAC.DatS,
FireDAC.DApt.Intf,
FireDAC.DApt,
Vcl.StdCtrls,
Data.DB,
FireDAC.Comp.DataSet,
FireDAC.Comp.Client,
Vcl.Mask,
Vcl.ExtCtrls,
Vcl.DBCtrls,
Vcl.Buttons,
Data.Bind.EngExt,
Vcl.Bind.DBEngExt,
Data.Bind.Components;
type
TForm5 = class(TForm)
FDConnection1: TFDConnection;
FDQuery1: TFDQuery;
ListBox1: TListBox;
FDQuery1Name: TStringField;
Edit1: TEdit;
Button1: TButton;
DataSource1: TDataSource;
FDQuery2: TFDQuery;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
DataSource2: TDataSource;
FDUpdateSQL2: TFDUpdateSQL;
DBNavigator1: TDBNavigator;
procedure Button1Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FDQuery2AfterInsert(DataSet: TDataSet);
procedure DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
procedure FDQuery2AfterDelete(DataSet: TDataSet);
private
{ Déclarations privées }
procedure filllistbox(const lastitem : String = '');
public
{ Déclarations publiques }
end;
var
Form5: TForm5;
implementation
{$R *.dfm}
uses system.math;
procedure TForm5.Button1Click(Sender: TObject);
begin
if Trim(edit1.Text)=EmptyStr
then FDQuery1.MacroByName('whereclause').Clear
else FDQuery1.MacroByName('whereclause').AsRaw:=Format('where name like ''%%%s%%''',[Trim(Edit1.text)]);
FDQuery1.Open();
FillListBox;
end;
procedure TForm5.DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
begin
case Button of
nbFirst: ListBox1.ItemIndex:=0;
nbPrior: ListBox1.ItemIndex:=MaxIntValue([0,ListBox1.ItemIndex-1]);
nbNext: ListBox1.ItemIndex:=MinIntValue([Listbox1.Items.Count-1,ListBox1.ItemIndex+1]);
nbLast: ListBox1.ItemIndex:=Listbox1.Items.Count-1;
nbInsert: Fdquery2.Insert;
nbDelete: try Fdquery2.delete; except end; // au cas où
end;
// se positionne
case Button of
nbFirst,
nbPrior,
nbNext,
nbLast: FDQuery2.Open('',[Listbox1.Items[Listbox1.ItemIndex]]);
end;
end;
procedure TForm5.FDQuery2AfterDelete(DataSet: TDataSet);
begin
filllistbox;
end;
procedure TForm5.FDQuery2AfterInsert(DataSet: TDataSet);
begin
filllistbox(FDquery2.FieldByName('name').asString);
end;
procedure TForm5.filllistbox(const lastitem : String = '');
begin
ListBox1.Items.Clear;
while not FDQuery1.EOF do
begin
ListBox1.Items.Add(FDQuery1.Fields[0].asString);
FDquery1.Next;
end;
FdQuery1.First; // se repositionner au début
if ListBox1.Items.Count>0 then
begin
if lastitem.IsEmpty then Listbox1.ItemIndex:=0
else ListBox1.ItemIndex:=ListBox1.Items.IndexOf(lastitem);
FDQuery2.Open('',[Listbox1.Items[Listbox1.ItemIndex]]);
end;
end;
procedure TForm5.FormCreate(Sender: TObject);
begin
FDQuery1.MacroByName('whereclause').Clear;
FDQuery1.Open;
FillListBox;
end;
procedure TForm5.ListBox1Click(Sender: TObject);
begin
FDQuery2.Open('',[Listbox1.Items[ListBox1.ItemIndex]]);
end;
end. |
Partager