-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompetitorFormUnit.pas
312 lines (280 loc) · 9.58 KB
/
CompetitorFormUnit.pas
1
2
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
unit CompetitorFormUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Buttons,DataModuleUnit, AddValueUnit;
type
TCompetitorForm = class(TForm)
ParticipationMark: TCheckBox;
FIOEdit: TLabeledEdit;
CancelBtn: TBitBtn;
OKBtn: TBitBtn;
GroupBox1: TGroupBox;
AgeBox: TComboBox;
GroupBox2: TGroupBox;
weightBox: TComboBox;
GroupBox4: TGroupBox;
beltBox: TComboBox;
GroupBox3: TGroupBox;
cityBox: TComboBox;
GroupBox5: TGroupBox;
clubBox: TComboBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure AgeBoxChange(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure OKBtnClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
// AddValueForm: TAddValueForm; //óíèâåðñàëüíàÿ ôîðìà äîáàâëåíèÿ
function load_list_to_weight_combo(age:string):integer; //çàêãðóçêà â êîìáîáîêñ âåñîâ
procedure add_new_value(formName,labelName,mask,tableName,colName, mes:string; var combo:TCombobox); //äîáàâëåíèå íîâîãî çíà÷åíèÿ â òàáëèöû èñõîäíûõ äàííûõ(êðîìå âåñà, îí îòäåëüíûé)
//formName-èìÿ îêíà,labelName-èìÿ ìåòêè,mask-ìàñêà ââîäà,tableName-èìÿ òàáëèöû èçìåíåíèÿ,colName - èìÿ ñòîëáöà èçìåíåíèé, mes - òåêñò ñîîáùåíèÿ ïðè îøèáêå îáíîâëåíèÿ íàáîðà äàííûõ â êîìáî; combo -èìÿ êîìáîáîêñà â êîòîðûé äîáàâèòñÿ èçìåíåíèå
public
{ Public declarations }
Function load_list_to_combo(tableName, colName: string; var Combo: TComboBox): integer; //çàïîëíÿåì êîìáîáîêñû/èìÿ òàáëèöû, èìÿ ñòîëáöà, èìÿ êîìáîáîêñà
function build_weights_list(age: string; var list:TStringList):integer; //çàïîëíåíèå ñïèñêà âåñîâ ïî âîçðàñòó/ 0-îê, 1-íå óäàëîñü ñäåëàòü çàïðîñ, 2-ïóñòàÿ òàáëèöà
end;
{var
CompetitorForm: TCompetitorForm; }
implementation
uses ADODB;
{$R *.dfm}
{ TCompetitorForm }
function TCompetitorForm.build_weights_list(age: string;
var list: TStringList): integer;
begin
list.Clear;
//äåëàåì îáúåäèíåííóþ òàáëèöó è âûáèðàåì òîëüêî âåñà
with DataModule1 do
begin
DBQuery.Close;
DBQuery.SQL.Text := 'SELECT t_weights.weight FROM t_weights INNER JOIN t_ages ON t_weights.age = t_ages.id'+
' WHERE t_ages.age = ' +QuotedStr(age);
try
DBQuery.Open;
if not DBQuery.IsEmpty then
begin
DBQuery.First;
while not DBQuery.Eof do
begin
list.Add(DBQuery.FieldByName('weight').AsString);
DBQuery.Next;
end;
result := 0;
end
else
result := 2;
except
result := 1;
end;
end;
DataModule1.DBQuery.Close;
end;
procedure TCompetitorForm.AgeBoxChange(Sender: TObject);
var list: TStringList; //÷åðåç íåãî ïåðåêèíåì ñïèñîê
begin
if AgeBox.Text = '' then //åñëè âûáðàíà âîçðàñòíàÿ, òî äåëàåì äîñòóïíûì âåñ
GroupBox2.Enabled := false
else
begin
GroupBox2.Enabled := true;
if load_list_to_weight_combo(AgeBox.Text) <> 0 then
ShowMessage('Ñïèñîê âåñîâ äëÿ äàííîé âîçðàñòíîé êàòåãîðèè ïóñò.');
end;
end;
function TCompetitorForm.load_list_to_combo(tableName, colName: string;
var Combo: TComboBox): integer;
begin
with DataModule1 do
begin
Combo.Clear;
DBQuery.Close;
DBQuery.SQL.Text :='SELECT * FROM ' + tablename;
try
DBQuery.Open;
if not DBQuery.IsEmpty then
begin
DBQuery.First;
While not DBQuery.Eof do //íåïóñòîé çàïðîñ, çíàû÷èò, çàïîëíÿåì ñòðèíãëèñò
begin
COMBO.Items.Add(DBQuery.FieldByName(colName).AsString);
DBQuery.Next;
end;
result:= 0;
end
else
result := 2;
except
result:= 1;
end;
DBQuery.Close;
end
end;
procedure TCompetitorForm.Button1Click(Sender: TObject); //äîáàâëåíèå âîçðàñòà
begin
add_new_value('Äîáàâèòü âîçðàñòíóþ êàòåãîðèþ','Âîçðàñòíàÿ êàòåãîðèÿ','','t_ages', 'age',
'Íå óäàëîñü îáíîâèòü ñïèñîê âåñîâûõ êàòåãîðèé. Âîçìîæíû íåïîëàäêè ñ áàçîé.',ageBox);
AgeBoxChange(nil); //èíèöèèðóåì èçìåíåíèå êîìáîáîêñà
{ AddValueForm := TAddValueForm.Create(self);
AddValueForm.set_values('Äîáàâèòü âîçðàñòíóþ êàòåãîðèþ','Âîçðàñòíàÿ êàòåãîðèÿ','###');
if AddValueForm.ShowModal = mrOK then
begin
if DataModule1.add_value_query('t_ages', 'age', trim(AddValueForm.ValueEdit.Text)) <> 0 then
ShowMessage('Íå óäàëîñü äîáàâèòü çíà÷åíèå â áàçó. Ïîïðîáóéòå åùå ðàç.')
else
begin
//îáíîâëÿåì ñîäåðæèìîå ñâîåãî êîìáîáîêñà è âåñà
if (load_list_to_combo('t_ages','age',ageBox) <> 0) then
showmessage('Íå óäàëîñü îáíîâèòü ñïèñîê âîçðàñòîâ. Âîçìîæíû íåïîëàäêè ñ áàçîé.');
//if (load_list_to_weight_combo(trim(AddValueForm.ValueEdit.Text)) <> 0) then
// showmessage('Íå óäàëîñü îáíîâèòü ñïèñîê âåñîâûõ êàòåãîðèé. Âîçìîæíû íåïîëàäêè ñ áàçîé.');
end
end;
AddValueForm.Destroy;
}
end;
function TCompetitorForm.load_list_to_weight_combo(age: string): integer;
var list:TstringList;
begin
list := TStringList.Create;
result := build_weights_list(age,list);
weightBox.Clear; //÷èñòèì îò ñòàðüÿ
weightBox.Items.Text := list.Text;
list.Destroy;
end;
procedure TCompetitorForm.Button2Click(Sender: TObject); //äîáàâëåíèå âåñà
var str:string;
begin
AddValueForm := TAddValueForm.Create(self);
AddValueForm.set_values('Äîáàâèòü âåñîâóþ êàòåãîðèþ','Âåñîâàÿ êàòåãîðèÿ âîçðàñòà ' + AgeBox.Text,'##c#');
if AddValueForm.ShowModal = mrOK then
begin
str := trim(AddValueForm.ValueEdit.Text);
//çàùèòà îò ïîâòîðíîãî ââîäà
if weightBox.Items.IndexOf(str) <> -1 then
begin
ShowMessage('Ââåäåííîå çíà÷åíèå óæå ïðèñóòñòâåóò â ñïèñêå');
//ñòàâèì åãî â òåêñò êîìáî
weightBox.ItemIndex := weightBox.Items.IndexOf(str);
end
else
if DataModule1.add_weight_query(str,AgeBox.Text) <> 0 then
ShowMessage('Íå óäàëîñü äîáàâèòü çíà÷åíèå â áàçó. Ïîïðîáóéòå åùå ðàç.')
else
begin
//îáíîâëÿåì ñîäåðæèìîå ñâîåãî êîìáîáîêñà âåñà
if (load_list_to_weight_combo(AgeBox.Text) <> 0) then
showmessage('Íå óäàëîñü îáíîâèòü ñïèñîê âåñîâûõ êàòåãîðèé. Âîçìîæíû íåïîëàäêè ñ áàçîé.')
else //åñëè îøèáîê íåò, òî âûñòàâëÿåì ââåäåííîå çíà÷åíèå âûáðàííûì
weightBox.ItemIndex := weightBox.Items.IndexOf(str);
end
end;
AddValueForm.Destroy;
end;
procedure TCompetitorForm.Button3Click(Sender: TObject); //äîáàâëåíèå êàòåãîðèè ïîÿñà
begin
add_new_value('Äîáàâèòü ïîÿñ','Öâåò ïîÿñà','','t_belts', 'belt',
'Íå óäàëîñü îáíîâèòü ñïèñîê ïîÿñîâ. Âîçìîæíû íåïîëàäêè ñ áàçîé.',beltBox);
end;
procedure TCompetitorForm.Button4Click(Sender: TObject); //äîáàâëåíèå íîâîãî ãîðîäà
begin
add_new_value('Äîáàâèòü ãîðîä','Íàçâàíèå ãîðîäà','','t_cities', 'city',
'Íå óäàëîñü îáíîâèòü ñïèñîê ãîðîäîâ. Âîçìîæíû íåïîëàäêè ñ áàçîé.',cityBox);
end;
procedure TCompetitorForm.Button5Click(Sender: TObject);
begin
add_new_value('Äîáàâèòü êëóá/òðåíåðà','Êëóá/òðåíåð','','t_clubs', 'club',
'Íå óäàëîñü îáíîâèòü ñïèñîê êëóáîâ. Âîçìîæíû íåïîëàäêè ñ áàçîé.',clubBox);
end;
procedure TCompetitorForm.FormCreate(Sender: TObject);
begin
//ñòàâèì ôîðìó ïî öåíòðó ýêðàíà
self.Left := (screen.Width - self.Width) div 2;
self.Top := (screen.Height - self.Height) div 2;
//Ãðóçèì âñå êîìáîáîêñû
load_list_to_combo('t_ages','age',ageBox);
load_list_to_combo('t_belts','belt',beltBox);
load_list_to_combo('t_cities', 'city', cityBox);
load_list_to_combo('t_clubs', 'club',clubBox);
end;
procedure TCompetitorForm.OKBtnClick(Sender: TObject);
begin
if Trim(FIOEdit.Text) = '' then
begin
ShowMessage('Íå çàïîëíåíû ÔÈÎ ó÷àñòíèêà');
Exit;
end;
if ageBox.Text = '' then
begin
ShowMessage('Íå âûáðàíà âîçðàñòíàÿ êàòåãîðèÿ');
Exit;
end;
if weightBox.Text = '' then
begin
ShowMessage('Íå âûáðàíà âåñîâàÿ êàòåãîðèÿ');
Exit;
end;
if BeltBox.Text = '' then
begin
ShowMessage('Íå âûáðàí ïîÿñ');
Exit;
end;
if cityBox.Text = '' then
begin
ShowMessage('Íå âûáðàí ãîðîä');
Exit;
end;
if clubBox.Text = '' then
begin
ShowMessage('Íå âûáðàí êëóá');
Exit;
end;
//åñëè äîøëè äî ñþäà, òî âñå îòëè÷íî
self.ModalResult := mrOk;
end;
procedure TCompetitorForm.add_new_value(formName, labelName, mask,
tableName, colName, mes: string; var combo: TCombobox);
var str: string; //ââåäåííîå çíà÷åíèå
begin
AddValueForm := TAddValueForm.Create(self);
//óñòàíàâëèâàåì íà÷àëüíûå çíà÷åíèÿ
AddValueForm.set_values(formName,labelName,mask);
if AddValueForm.ShowModal = mrOK then //ïîëüçîâàòåëü ïîäòâåðäèë ââîä
begin
str := trim(AddValueForm.ValueEdit.Text);
//çàùèòà îò ïîâòîðíîãî ââîäà
if combo.Items.IndexOf(str) <> -1 then
begin
ShowMessage('Ââåäåííîå çíà÷åíèå óæå ïðèñóòñòâåóò â ñïèñêå');
combo.ItemIndex := combo.Items.IndexOf(str);
end
else
if DataModule1.add_value_query(tableName, colName, str) <> 0 then
ShowMessage('Íå óäàëîñü äîáàâèòü çíà÷åíèå â áàçó. Ïîïðîáóéòå åùå ðàç.')
else
begin
//îáíîâëÿåì ñîäåðæèìîå ñâîåãî êîìáîáîêñà
if (load_list_to_combo(tableName, colName, combo) <> 0) then // åñëè âîçíèêëà îøèáêà îáíîâëåíèÿ
showmessage(mes)
else //åñëè îøèáîê íåò, òî âûñòàâëÿåì ââåäåííîå çíà÷åíèå âûáðàííûì
combo.ItemIndex := combo.Items.IndexOf(str);
end
end;
AddValueForm.Destroy;
end;
procedure TCompetitorForm.FormShow(Sender: TObject);
begin
FIOEdit.SetFocus;
FIOEdit.SelectAll;
end;
end.