source: trunk/Forms/UFormRecord.pas

Last change on this file was 29, checked in by chronos, 20 months ago
  • Fixed: Load table fields in records list and record edit form.
  • Fixed: Record add needs to insert new row into database.
File size: 6.1 KB
Line 
1unit UFormRecord;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
7 ComCtrls, ActnList, StdCtrls, EditBtn, UDatabase, Spin, Generics.Collections;
8
9type
10
11 { TFormRecord }
12
13 TFormRecord = class(TForm)
14 ASave: TAction;
15 ACancel: TAction;
16 ActionList1: TActionList;
17 ButtonOk: TButton;
18 ButtonCancel: TButton;
19 Panel1: TPanel;
20 procedure ACancelExecute(Sender: TObject);
21 procedure ASaveExecute(Sender: TObject);
22 procedure FormCreate(Sender: TObject);
23 procedure FormDestroy(Sender: TObject);
24 procedure FormShow(Sender: TObject);
25 public
26 Table: TTable;
27 Row: TRecord;
28 Controls: TObjectList<TControl>;
29 Labels: TObjectList<TControl>;
30 procedure ReloadControls;
31 procedure Load(DataRecord: TRecord);
32 procedure Save(DataRecord: TRecord);
33 end;
34
35
36implementation
37
38uses
39 UDataTypes, UCore;
40
41{$R *.lfm}
42
43resourcestring
44 STableRecordEdit = 'Table record edit';
45
46{ TFormRecord }
47
48procedure TFormRecord.FormShow(Sender: TObject);
49begin
50 Caption := STableRecordEdit + ' - ' + Table.Caption;
51 ReloadControls;
52end;
53
54procedure TFormRecord.ACancelExecute(Sender: TObject);
55begin
56 ModalResult := mrCancel;
57end;
58
59procedure TFormRecord.ASaveExecute(Sender: TObject);
60begin
61 ModalResult := mrOk;
62end;
63
64procedure TFormRecord.FormCreate(Sender: TObject);
65begin
66 Controls := TObjectList<TControl>.Create;
67 Labels := TObjectList<TControl>.Create;
68end;
69
70procedure TFormRecord.FormDestroy(Sender: TObject);
71begin
72 FreeAndNil(Labels);
73 FreeAndNil(Controls);
74end;
75
76procedure TFormRecord.ReloadControls;
77begin
78end;
79
80procedure TFormRecord.Load(DataRecord: TRecord);
81var
82 I: Integer;
83 NewLabel: TLabel;
84 NewControl: TControl;
85 CellRect: TRect;
86const
87 LabelWidth = 300;
88var
89 LineHeight: Integer;
90begin
91 LineHeight := Core.ScaleDPI1.ScaleY(35, Core.ScaleDPI1.DesignDPI.Y);
92
93 Row := DataRecord;
94 Controls.Clear;
95 Labels.Clear;
96 for I := 0 to Table.Fields.Count - 1 do begin
97 CellRect := Rect(10, 10 + I * LineHeight, Panel1.Width - 20, (I + 1) * LineHeight);
98 NewLabel := TLabel.Create(Panel1);
99 NewLabel.Caption := Table.Fields[I].TextBefore;
100 NewLabel.Parent := Panel1;
101 NewLabel.Left := CellRect.Left;
102 NewLabel.Top := CellRect.Top;
103 NewLabel.Visible := True;
104 Labels.Add(NewLabel);
105 case Table.Fields[I].DataType.FieldType of
106 ftString: begin
107 NewControl := TEdit.Create(Panel1);
108 NewControl.Anchors := [akLeft, akTop, akRight];
109 NewControl.Parent := Panel1;
110 NewControl.Left := CellRect.Left + LabelWidth;
111 NewControl.Top := CellRect.Top;
112 NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
113 NewControl.Visible := True;
114 TEdit(NewControl).Text := TValueString(Row.Values[I]).Value;
115 Controls.Add(NewControl);
116 end;
117 ftInteger: begin
118 NewControl := TSpinEdit.Create(Panel1);
119 NewControl.Parent := Panel1;
120 NewControl.Left := CellRect.Left + LabelWidth;
121 NewControl.Top := CellRect.Top;
122 NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
123 NewControl.Visible := True;
124 TSpinEdit(NewControl).Value := TValueInteger(Row.Values[I]).Value;
125 Controls.Add(NewControl);
126 end;
127 ftDateTime: begin
128 NewControl := TDateEdit.Create(Panel1);
129 NewControl.Parent := Panel1;
130 NewControl.Left := CellRect.Left + LabelWidth;
131 NewControl.Top := CellRect.Top;
132 NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
133 NewControl.Visible := True;
134 TDateEdit(NewControl).Date := TValueDateTime(Row.Values[I]).Value;
135 Controls.Add(NewControl);
136 end;
137 ftBoolean: begin
138 NewControl := TCheckBox.Create(Panel1);
139 NewControl.Parent := Panel1;
140 NewControl.Left := CellRect.Left + LabelWidth;
141 NewControl.Top := CellRect.Top;
142 NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
143 NewControl.Visible := True;
144 TCheckBox(NewControl).Checked := TValueBoolean(Row.Values[I]).Value;
145 Controls.Add(NewControl);
146 end;
147 ftFloat: begin
148 NewControl := TFloatSpinEdit.Create(Panel1);
149 NewControl.Parent := Panel1;
150 NewControl.Left := CellRect.Left + LabelWidth;
151 NewControl.Top := CellRect.Top;
152 NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
153 NewControl.Visible := True;
154 TFloatSpinEdit(NewControl).Value := TValueFloat(Row.Values[I]).Value;
155 Controls.Add(NewControl);
156 end;
157 ftImage: begin
158 NewControl := TImage.Create(Panel1);
159 NewControl.Parent := Panel1;
160 NewControl.Left := CellRect.Left + LabelWidth;
161 NewControl.Top := CellRect.Top;
162 NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
163 NewControl.Height := NewLabel.Height;
164 NewControl.Visible := True;
165 //TImage(NewControl).Value := TValueFloat(Row.Values[I]).Value;
166 Controls.Add(NewControl);
167 end;
168 ftReference: begin
169 NewControl := TComboBox.Create(Panel1);
170 NewControl.Parent := Panel1;
171 NewControl.Left := CellRect.Left + LabelWidth;
172 NewControl.Top := CellRect.Top;
173 NewControl.Width := CellRect.Right - CellRect.Left - LabelWidth;
174 NewControl.Height := NewLabel.Height;
175 NewControl.Visible := True;
176
177 TComboBox(NewControl).Items.Add(IntToStr(TValueReference(Row.Values[I]).Value));
178 Controls.Add(NewControl);
179 end;
180 end;
181 end;
182end;
183
184procedure TFormRecord.Save(DataRecord: TRecord);
185var
186 I: Integer;
187begin
188 for I := 0 to Table.Fields.Count - 1 do begin
189 case Table.Fields[I].DataType.FieldType of
190 ftString: TValueString(Row.Values[I]).Value := TEdit(Controls[I]).Text;
191 ftInteger: TValueInteger(Row.Values[I]).Value := TSpinEdit(Controls[I]).Value;
192 ftDateTime: TValueDateTime(Row.Values[I]).Value := TDateEdit(Controls[I]).Date;
193 ftBoolean: TValueBoolean(Row.Values[I]).Value := TCheckBox(Controls[I]).Checked;
194 ftFloat: TValueFloat(Row.Values[I]).Value := TFloatSpinEdit(Controls[I]).Value;
195 //ftImage: TValueFloat(Row.Values[I]).Value := TFloatSpinEdit(Controls[I]).Value;
196 end;
197 end;
198end;
199
200end.
201
Note: See TracBrowser for help on using the repository browser.