| 12
 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
 
 |  
var
  MainForm: TMainForm;
  FCon  : TPQConnection;
  FTrs  : TSQLTransaction;
  Libelles: array [0..100] of TLabel;
  Champs  : array [0..100] of TWinControl;
  sqlSRPList: string = 'select srpnum as "ID", srpnam as "Designation" from salesrep order by "ID";';
  sqlSRPForm: String ='select srpnum as "&Identifiant", srpnam as "&Nom",'+
                     'datein as "Date entrée", dateout as "Date de sortie",'+
                     'isactive as "Actif" from salesrep;';
 
procedure TMainForm.FormCreate(Sender: TObject);
begin
  FCon := TPQConnection.Create(self);
  FCon.DatabaseName:= '********';
  FCon.UserName    := 'postgres';
  FCon.HostName    := '**********';
  FCon.Password    := '*********';
 
  FTrs := TSQLTransaction.Create(MainForm);
  FTrs.DataBase := FCon;
end;
 
function TMainForm.GetQuery : TSQLQuery;
  var AQuery : TSQLQuery;
begin
  AQuery := TSQLQuery.Create(MainForm);
  AQuery.Database    := FCon;
  AQuery.Transaction := FTrs;
  Result             := AQuery;
end;
 
procedure TMainForm.BuildForm(panel: TGroupBox; sql: string);
var
  i, ColCount, LabelWidth : integer;
  dataset                 : TSQLQuery;
begin
  LabelWidth:=0;
  if panel.ComponentCount>0 then exit;;
  //Initialiser la requete
  dataset     := GetQuery;
  dataset.SQL.Text := sql;
  dataset.Open;
 
  colcount :=dataset.Fields.Count;     //Le nombre de colonnes
 
  //Loop 1:
  //Calculer la largeur des libélles
  For i:=0 to colcount-1 do begin
    if LabelWidth < Canvas.TextWidth(dataset.Fields[i].FieldName) then
       LabelWidth:= Canvas.TextWidth(dataset.Fields[i].FieldName)+10;
  end;
 
  //Loop 2:
  for i:=0 to colcount-1 do begin
    //Création des champs
    case dataset.FieldByName(dataset.Fields[i].FieldName).DataType of
      ftString:begin
          Champs[i] := TmzEdit.Create(panel);            //Crée champ
          Champs[i].width := dataset.Fields[i].Size*11;  //Fixe largeur
          TmzEdit(Champs[i]).MaxLength:=dataset.Fields[i].Size;//Fixe taille saisie
        end;
      ftBoolean     :Champs[i] := TCheckBox.Create(panel);//Crée champ à cocher
      ftdate        :Champs[i] := TmzDateTime.Create(panel);//Crée champ date
    end;
 
    with Champs[i] do
    begin
      Parent := panel;
      Top    := 5+(20*i);
      Left   := LabelWidth;
    end;
 
    //Création des libéllés
    Libelles[i] := TLabel.Create(panel);//Créer les libellés
    with Libelles[i] do
    begin
      Parent := panel;
      Top    := 5+(20*i);
      Left   := 5;
      Width  := LabelWidth;
      Caption:=dataset.Fields[i].FieldName;
      FocusControl:=Champs[i];      //Affecte raccourci clavier au champ adequat
    end;
  end;
  panel.Height:=((panel.ComponentCount div 2)+1)*20;
 
  dataset.Close;
  dataset.Free;
end; | 
Partager