Ignore:
Timestamp:
Jan 18, 2015, 5:25:37 PM (10 years ago)
Author:
chronos
Message:
  • Added: Support for editing String and DateTime value types in record edit form.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        33MyData.lps
        44data.xml
         5Config.xml
  • trunk/Forms/UFormRecord.pas

    r2 r3  
    66
    77uses
    8   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, UDatabase;
     8  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
     9  ComCtrls, ActnList, StdCtrls, EditBtn, UDatabase, Contnrs;
    910
    1011type
     12
     13  { TFormRecord }
     14
    1115  TFormRecord = class(TForm)
     16    ASave: TAction;
     17    ACancel: TAction;
     18    ActionList1: TActionList;
     19    ButtonOk: TButton;
     20    ButtonCancel: TButton;
     21    Panel1: TPanel;
     22    procedure ACancelExecute(Sender: TObject);
     23    procedure ASaveExecute(Sender: TObject);
     24    procedure FormCreate(Sender: TObject);
     25    procedure FormDestroy(Sender: TObject);
     26    procedure FormShow(Sender: TObject);
    1227  private
    1328    { private declarations }
     
    1530    Table: TTable;
    1631    Row: TRecord;
     32    Controls: TObjectList; // TListObject<TControl>
     33    Labels: TObjectList; // TListObject<TControl>
     34    procedure ReloadControls;
     35    procedure Load(DataRecord: TRecord);
     36    procedure Save(DataRecord: TRecord);
    1737  end;
    1838
     
    2444{$R *.lfm}
    2545
     46{ TFormRecord }
     47
     48procedure TFormRecord.FormShow(Sender: TObject);
     49begin
     50  ReloadControls;
     51end;
     52
     53procedure TFormRecord.ACancelExecute(Sender: TObject);
     54begin
     55  ModalResult := mrCancel;
     56  Close;
     57end;
     58
     59procedure TFormRecord.ASaveExecute(Sender: TObject);
     60begin
     61  ModalResult := mrOk;
     62  Close;
     63end;
     64
     65procedure TFormRecord.FormCreate(Sender: TObject);
     66begin
     67  Controls := TObjectList.Create;
     68  Labels := TObjectList.Create;
     69end;
     70
     71procedure TFormRecord.FormDestroy(Sender: TObject);
     72begin
     73  Labels.Free;
     74  Controls.Free;
     75end;
     76
     77procedure TFormRecord.ReloadControls;
     78begin
     79end;
     80
     81procedure TFormRecord.Load(DataRecord: TRecord);
     82var
     83  I: Integer;
     84  NewLabel: TLabel;
     85  NewControl: TControl;
     86  CellRect: TRect;
     87begin
     88  Row := DataRecord;
     89  Controls.Clear;
     90  Labels.Clear;
     91  for I := 0 to Table.Fields.Count - 1 do begin
     92    CellRect := Rect(10, 10 + I * 70, Panel1.Width - 20, (I + 1) * 70);
     93    NewLabel := TLabel.Create(Panel1);
     94    NewLabel.Caption := TField(Table.Fields[I]).TextBefore;
     95    NewLabel.Parent := Panel1;
     96    NewLabel.Left := CellRect.Left;
     97    NewLabel.Top := CellRect.Top;
     98    NewLabel.Visible := True;
     99    Labels.Add(NewLabel);
     100    case TField(Table.Fields[I]).FieldType of
     101      ftString: begin
     102        NewControl := TEdit.Create(Panel1);
     103        NewControl.Parent := Panel1;
     104        NewControl.Left := CellRect.Left;
     105        NewControl.Top := CellRect.Top + NewLabel.Height + 6;
     106        NewControl.Width := CellRect.Right - CellRect.Left;
     107        NewControl.Visible := True;
     108        TEdit(NewControl).Text := TValueString(Row.Values[I]).Value;
     109        Controls.Add(NewControl);
     110      end;
     111      ftDateTime: begin
     112        NewControl := TDateEdit.Create(Panel1);
     113        NewControl.Parent := Panel1;
     114        NewControl.Left := CellRect.Left;
     115        NewControl.Top := CellRect.Top + NewLabel.Height + 6;
     116        NewControl.Width := CellRect.Right - CellRect.Left;
     117        NewControl.Visible := True;
     118        TDateEdit(NewControl).Date := TValueDateTime(Row.Values[I]).Value;
     119        Controls.Add(NewControl);
     120      end;
     121    end;
     122  end;
     123end;
     124
     125procedure TFormRecord.Save(DataRecord: TRecord);
     126var
     127  I: Integer;
     128begin
     129  for I := 0 to Table.Fields.Count - 1 do begin
     130    case TField(Table.Fields[I]).FieldType of
     131      ftString: TValueString(Row.Values[I]).Value := TEdit(Controls[I]).Text;
     132      ftDateTime: TValueDateTime(Row.Values[I]).Value := TDateEdit(Controls[I]).Date;
     133    end;
     134  end;
     135end;
     136
    26137end.
    27138
Note: See TracChangeset for help on using the changeset viewer.