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