-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBindListUtils.pas
More file actions
265 lines (225 loc) · 8.31 KB
/
BindListUtils.pas
File metadata and controls
265 lines (225 loc) · 8.31 KB
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
//---------------------------------------------------------------------------
// This software is Copyright (c) 2011 Embarcadero Technologies, Inc.
// You may only use this software if you are an authorized licensee
// of Delphi, C++Builder or RAD Studio (Embarcadero Products).
// This software is considered a Redistributable as defined under
// the software license agreement that comes with the Embarcadero Products
// and is subject to that software license agreement.
//---------------------------------------------------------------------------
unit BindListUtils;
// Utility methods to populate TListBox and TComboBox controls using TBindList
// When using with FireMonkey, the project must use FMX.Bind.Editors
// When using with VCL, the project must use VCL.Bind.Editor
interface
uses System.Classes, Data.Bind.Components, Data.Db;
// Clear a list
procedure ClearList(AControl: TComponent);
// Fill a list with an enumerable object such as TList<>, using one or more binding expressions
procedure FillList(AControl: TComponent; const AControlExpressions: TArray<string>;
ASource: TObject; const ASourceExpressions: TArray<string>; const ASourceMemberName: string = ''); overload;
// Fill a list with an enumerable object such as TList<>, using a single binding expression
procedure FillList(AControl: TComponent; const AControlExpression: string;
ASource: TObject; const ASourceExpression: string; const ASourceMemberName: string = ''); overload;
// Fill a list from a binding scope, using one or more binding expressions
procedure FillList(AControl: TComponent; const AControlExpressions: TArray<string>;
ASource: TBaseBindScopeComponent; const ASourceExpressions: TArray<string>; const ASourceMemberName: string = ''); overload;
// Fill a list from a binding scope, using a single binding expression
procedure FillList(AControl: TComponent; const AControlExpression: string;
ASource: TBaseBindScopeComponent; const ASourceExpression: string; const ASourceMemberName: string = ''); overload;
// Fill a list from a TDataSet, using one or more binding expression. Field name is optional.
procedure FillList(AControl: TComponent; const AControlExpressions: TArray<string>;
ASource: TDataSet; const ASourceExpressions: TArray<string>; const AFieldName: string = ''); overload;
// Fill a list from a TDataSet, using a single binding expression. Field name is optional.
procedure FillList(AControl: TComponent; const AControlExpression: string;
ASource: TDataSet; const ASourceExpression: string; const AFieldName: string = ''); overload;
implementation
type
// Enumerate the records in a TDataSet. Current is TDataSet
TDataSetEnumerator = class
private
FDataSet: TDataSet;
FFirst: Boolean;
public
constructor Create(ADataSet: TDataSet);
function MoveNext: Boolean;
property Current: TDataSet read FDataSet;
end;
// Enumerate the records in a TDataSet. Current is a TField
TDataSetFieldEnumerator = class
private
FField: TField;
FFirst: Boolean;
public
constructor Create(AField: TField);
function MoveNext: Boolean;
property Current: TField read FField;
end;
// Present a TDataSet as an enumerable type
TDataSetEnumerable = class
private
FDataSet: TDataSet;
public
constructor Create(ADataSet: TDataSet);
function GetEnumerator: TDataSetEnumerator;
end;
// Present a TDataSet as an enumerable type
TDataSetFieldEnumerable = class
private
FField: TField;
public
constructor Create(AField: TField);
function GetEnumerator: TDataSetFieldEnumerator;
end;
procedure ClearList(AControl: TComponent);
var
LBindList: TBindList;
begin
LBindList := TBindList.Create(nil);
try
// Turn off auto properties.
LBindList.AutoFill := False;
LBindList.AutoActivate := False;
LBindList.ControlComponent := AControl;
LBindList.ClearList;
finally
LBindList.Free;
end;
end;
procedure FillList(AControl: TComponent; const AControlExpression: string;
ASource: TBaseBindScopeComponent; const ASourceExpression, ASourceMemberName: string); overload;
begin
FillList(AControl, TArray<string>.Create(AControlExpression),
ASource, TArray<string>.Create(ASourceExpression), ASourceMemberName);
end;
procedure FillList(AControl: TComponent; const AControlExpressions: TArray<string>;
ASource: TBaseBindScopeComponent; const ASourceExpressions: TArray<string>; const ASourceMemberName: string); overload;
var
LBindList: TBindList;
I: Integer;
begin
LBindList := TBindList.Create(nil);
try
// Turn off auto properties.
LBindList.AutoFill := False;
LBindList.AutoActivate := False;
LBindList.ControlComponent := AControl;
LBindList.SourceComponent := ASource;
LBindList.SourceMemberName := ASourceMemberName;
for I := Low(AControlExpressions) to High(AControlExpressions) do
with LBindList.FormatExpressions.AddExpression do
begin
SourceExpression := ASourceExpressions[I];
ControlExpression := AControlExpressions[I];
end;
LBindList.FillList;
finally
LBindList.Free;
end;
end;
procedure FillList(AControl: TComponent; const AControlExpression: string;
ASource: TObject; const ASourceExpression, ASourceMemberName: string); overload;
begin
FillList(AControl, TArray<string>.Create(AControlExpression),
ASource, TArray<string>.Create(ASourceExpression), ASourceMemberName);
end;
procedure FillList(AControl: TComponent; const AControlExpressions: TArray<string>;
ASource: TObject; const ASourceExpressions: TArray<string>; const ASourceMemberName: string); overload;
var
LScope: TBindScope;
begin
LScope := TBindScope.Create(nil);
try
LScope.DataObject := ASource;
FillList(AControl, AControlExpressions, LScope, ASourceExpressions, ASourceMemberName);
finally
LScope.Free;
end;
end;
procedure FillList(AControl: TComponent; const AControlExpression: string;
ASource: TDataSet; const ASourceExpression, AFieldName: string); overload;
begin
FillList(AControl, TArray<string>.Create(AControlExpression),
ASource, TArray<string>.Create(ASourceExpression), AFieldName);
end;
procedure FillList(AControl: TComponent; const AControlExpressions: TArray<string>;
ASource: TDataSet; const ASourceExpressions: TArray<string>; const AFieldName: string); overload;
var
LEnumerable: TObject;
LScope: TBindScope;
begin
LEnumerable := nil;
LScope := nil;
try
if AFieldName = '' then
// Current will be TDataSet
LEnumerable := TDataSetEnumerable.Create(ASource)
else
// Current will be TField
LEnumerable := TDataSetFieldEnumerable.Create(ASource.FieldByName(AFieldName));
LScope := TBindScope.Create(nil);
LScope.DataObject := LEnumerable;
FillList(AControl, AControlExpressions,
LScope, ASourceExpressions);
finally
LScope.Free;
LEnumerable.Free;
end;
end;
procedure FillListBox(AControl: TComponent;
ASource: TObject; const ASourceExpression: string);
begin
FillList(AControl, 'Text', ASource, ASourceExpression);
end;
{ TDataSetEnumerable }
constructor TDataSetEnumerable.Create(ADataSet: TDataSet);
begin
FDataSet := ADataSet;
end;
function TDataSetEnumerable.GetEnumerator: TDataSetEnumerator;
begin
Result := TDataSetEnumerator.Create(FDataSet);
end;
{ TDataSetEnumerator }
constructor TDataSetEnumerator.Create(ADataSet: TDataSet);
begin
FDataSet := ADataSet;
FFirst := True;
end;
function TDataSetEnumerator.MoveNext: Boolean;
begin
if FFirst then
begin
FFirst := False;
FDataSet.First;
end
else
FDataSet.Next;
Result := not FDataSet.Eof;
end;
{ TDataSetFieldEnumerable }
constructor TDataSetFieldEnumerable.Create(AField: TField);
begin
FField := AField;
end;
function TDataSetFieldEnumerable.GetEnumerator: TDataSetFieldEnumerator;
begin
Result := TDataSetFieldEnumerator.Create(FField);
end;
{ TDataSetFieldEnumerator }
constructor TDataSetFieldEnumerator.Create(AField: TField);
begin
FFirst := True;
FField := AField;
end;
function TDataSetFieldEnumerator.MoveNext: Boolean;
begin
if FFirst then
begin
FFirst := False;
FField.DataSet.First;
end
else
FField.DataSet.Next;
Result := not FField.DataSet.Eof;
end;
end.