使用Delphi Firemonkey的组合框中的键/值对
问题描述:
我想使用枚举器来填充具有键/值对的组合框。重要的是我隐藏用户的密钥并仅显示值。在选择时,我想捕获与所选值关联的密钥。使用Delphi Firemonkey的组合框中的键/值对
该代码看起来与此类似。
var
currentObj: ISuperObject;
enum: TSuperEnumerator<IJSONAncestor>;
while enum.MoveNext do
begin
currentObj := enum.Current.AsObject;
cboUserList.Items.Add(currentObj.S['key'],currentObj.S['value']);
end;
关键currentObj.S [“键”]应捕获的用户选择的值 currentObj.S [“值”]这是为了在cboUserList对用户可见的下拉列表。
任何想法?
答
一个简单的跨平台解决方案是使用一个单独的TStringList
举行key
S,然后在组合框中显示value
,并使用其项目索引访问TStringList
项目。
var
currentObj: ISuperObject;
enum: TSuperEnumerator<IJSONAncestor>;
while enum.MoveNext do
begin
currentObj := enum.Current.AsObject;
userSL.Add(currentObj.S['key']);
cboUserList.Items.Add(currentObj.S['value']);
end;
var
index: Integer;
key: string;
begin
index := cboUserList.ItemIndex;
key := userSL[index];
...
end;
答
你可以在课堂上包装你的钥匙,例如
type
TKey = class
S: string;
constructor Create(const AStr: string);
end;
constructor TKey.Create(const AStr: string);
begin
S := AStr;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
ComboBox1.Items.AddObject('value', TKey.Create('key'));
end;
,然后访问它作为
procedure TForm2.ComboBox1Change(Sender: TObject);
begin
Caption := (ComboBox1.Items.Objects[ComboBox1.ItemIndex] as TKey).S;
end;
只是确保摧毁这些物体后
+2
我会避免使用可视化组件作为业务逻辑的一部分。 –
不要让一个GUI控制管理程序的数据结构。 –