1 | unit FormRecords;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
---|
7 | ActnList, Menus, DbEngine, FormEx;
|
---|
8 |
|
---|
9 | type
|
---|
10 |
|
---|
11 | { TFormRecords }
|
---|
12 |
|
---|
13 | TFormRecords = class(TFormEx)
|
---|
14 | AAdd: TAction;
|
---|
15 | ActionList1: TActionList;
|
---|
16 | AModify: TAction;
|
---|
17 | ARemove: TAction;
|
---|
18 | ListViewRecords: TListView;
|
---|
19 | MenuItem1: TMenuItem;
|
---|
20 | MenuItem2: TMenuItem;
|
---|
21 | MenuItem3: TMenuItem;
|
---|
22 | PopupMenu1: TPopupMenu;
|
---|
23 | ToolBar1: TToolBar;
|
---|
24 | ToolButton1: TToolButton;
|
---|
25 | ToolButton2: TToolButton;
|
---|
26 | ToolButton3: TToolButton;
|
---|
27 | procedure AAddExecute(Sender: TObject);
|
---|
28 | procedure AModifyExecute(Sender: TObject);
|
---|
29 | procedure ARemoveExecute(Sender: TObject);
|
---|
30 | procedure FormShow(Sender: TObject);
|
---|
31 | procedure ListViewRecordsData(Sender: TObject; Item: TListItem);
|
---|
32 | procedure ListViewRecordsSelectItem(Sender: TObject; Item: TListItem;
|
---|
33 | Selected: Boolean);
|
---|
34 | public
|
---|
35 | Table: TTable;
|
---|
36 | procedure UpdateInterface;
|
---|
37 | procedure ReloadList;
|
---|
38 | end;
|
---|
39 |
|
---|
40 |
|
---|
41 | implementation
|
---|
42 |
|
---|
43 | uses
|
---|
44 | FormRecord;
|
---|
45 |
|
---|
46 | {$R *.lfm}
|
---|
47 |
|
---|
48 | resourcestring
|
---|
49 | STable = 'Table';
|
---|
50 | SRemoveRecord = 'Remove record';
|
---|
51 | SRemoveRecordConfirm = 'Do you want to remove record?';
|
---|
52 |
|
---|
53 | { TFormRecords }
|
---|
54 |
|
---|
55 | procedure TFormRecords.FormShow(Sender: TObject);
|
---|
56 | var
|
---|
57 | I: Integer;
|
---|
58 | begin
|
---|
59 | for I := 0 to ToolBar1.ButtonCount - 1 do
|
---|
60 | ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
|
---|
61 | Caption := STable + ' - ' + Table.Caption;
|
---|
62 | Table.Fields.Load;
|
---|
63 | ReloadList;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | procedure TFormRecords.ListViewRecordsData(Sender: TObject; Item: TListItem);
|
---|
67 | var
|
---|
68 | I: Integer;
|
---|
69 | begin
|
---|
70 | if (Item.Index >= 0) and (Item.Index < Table.Records.Count) then
|
---|
71 | with Table.Records[Item.Index] do begin
|
---|
72 | for I := 0 to Table.Fields.Count - 1 do begin
|
---|
73 | if I = 0 then Item.Caption := Values[0].GetString
|
---|
74 | else Item.SubItems.Add(Values[I].GetString);
|
---|
75 | end;
|
---|
76 | Item.Data := Table.Records[Item.Index];
|
---|
77 | end
|
---|
78 | end;
|
---|
79 |
|
---|
80 | procedure TFormRecords.ListViewRecordsSelectItem(Sender: TObject; Item: TListItem;
|
---|
81 | Selected: Boolean);
|
---|
82 | begin
|
---|
83 | UpdateInterface;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TFormRecords.AModifyExecute(Sender: TObject);
|
---|
87 | begin
|
---|
88 | if Assigned(ListViewRecords.Selected) then begin
|
---|
89 | with TFormRecord.Create(nil) do
|
---|
90 | try
|
---|
91 | Table := Self.Table;
|
---|
92 | Load(TRecord(ListViewRecords.Selected.Data));
|
---|
93 | if ShowModal = mrOk then begin
|
---|
94 | Save(TRecord(ListViewRecords.Selected.Data));
|
---|
95 | ReloadList;
|
---|
96 | end;
|
---|
97 | finally
|
---|
98 | Free;
|
---|
99 | end;
|
---|
100 | end;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | procedure TFormRecords.ARemoveExecute(Sender: TObject);
|
---|
104 | begin
|
---|
105 | if Assigned(ListViewRecords.Selected) then
|
---|
106 | if MessageDlg(SRemoveRecord, SRemoveRecordConfirm,
|
---|
107 | mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
|
---|
108 | Table.Records.Remove(ListViewRecords.Selected.Data);
|
---|
109 | ReloadList;
|
---|
110 | end;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure TFormRecords.AAddExecute(Sender: TObject);
|
---|
114 | var
|
---|
115 | NewRecord: TRecord;
|
---|
116 | ValueClass: TValueClass;
|
---|
117 | I: Integer;
|
---|
118 | NewValue: TValue;
|
---|
119 | Columns: string;
|
---|
120 | Values: string;
|
---|
121 | begin
|
---|
122 | NewRecord := TRecord.Create;
|
---|
123 | NewRecord.Table := Table;
|
---|
124 | NewRecord.Values.Count := Table.Fields.Count;
|
---|
125 | for I := 0 to Table.Fields.Count - 1 do begin
|
---|
126 | ValueClass := Table.Fields[I].GetValueClass;
|
---|
127 | NewValue := ValueClass.Create;
|
---|
128 | NewRecord.Values[I] := NewValue;
|
---|
129 | end;
|
---|
130 |
|
---|
131 | with TFormRecord.Create(nil) do
|
---|
132 | try
|
---|
133 | Table := Self.Table;
|
---|
134 | Load(NewRecord);
|
---|
135 | if ShowModal = mrOk then begin
|
---|
136 | Save(NewRecord);
|
---|
137 |
|
---|
138 | Values := '';
|
---|
139 | Columns := '';
|
---|
140 | for I := 0 to Table.Fields.Count - 1 do begin
|
---|
141 | Columns := Columns + Table.Fields[I].Name;
|
---|
142 | Values := Values + '"' + NewRecord.Values[I].GetString + '"';
|
---|
143 | if I < Table.Fields.Count - 1 then begin
|
---|
144 | Columns := Columns + ' , ';
|
---|
145 | Values := Values + ' , ';
|
---|
146 | end;
|
---|
147 | end;
|
---|
148 | Table.DbClient.Query('INSERT INTO ' + Table.Name + ' ( ' + Columns + ' )' +
|
---|
149 | ' VALUES ( ' + Values + ' )');
|
---|
150 |
|
---|
151 | NewRecord.Free;
|
---|
152 | ReloadList;
|
---|
153 | end else NewRecord.Free;
|
---|
154 | finally
|
---|
155 | Free;
|
---|
156 | end;
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure TFormRecords.UpdateInterface;
|
---|
160 | begin
|
---|
161 | AModify.Enabled := Assigned(ListViewRecords.Selected);
|
---|
162 | ARemove.Enabled := Assigned(ListViewRecords.Selected);
|
---|
163 | end;
|
---|
164 |
|
---|
165 | procedure TFormRecords.ReloadList;
|
---|
166 | var
|
---|
167 | I: Integer;
|
---|
168 | begin
|
---|
169 | Table.Records.Load;
|
---|
170 |
|
---|
171 | ListViewRecords.Columns.BeginUpdate;
|
---|
172 | try
|
---|
173 | while ListViewRecords.Columns.Count > Table.Fields.Count do
|
---|
174 | ListViewRecords.Columns[ListViewRecords.ColumnCount - 1].Free;
|
---|
175 | while ListViewRecords.Columns.Count < Table.Fields.Count do
|
---|
176 | ListViewRecords.Columns.Add;
|
---|
177 | for I := 0 to Table.Fields.Count - 1 do begin
|
---|
178 | ListViewRecords.Columns[I].Caption := Table.Fields[I].TextBefore;
|
---|
179 | ListViewRecords.Columns[I].Width := 200;
|
---|
180 | end;
|
---|
181 | finally
|
---|
182 | ListViewRecords.Columns.EndUpdate;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | ListViewRecords.Items.Count := Table.Records.Count;
|
---|
186 | ListViewRecords.Repaint;
|
---|
187 | end;
|
---|
188 |
|
---|
189 | end.
|
---|
190 |
|
---|