使用 Delphi Firemonkey 在 ComboBox 中 Key/Value 对
Key/Value pairs in ComboBox using Delphi Firemonkey
我想使用枚举器来填充具有 Key/Value 对的组合框。重要的是我对用户隐藏密钥并仅显示值。在 selecting 上,我想捕获与 selected 值关联的密钥。
代码看起来与此类似。
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['key'] 应该在值的用户 select 上捕获
currentObj.S['value'] 在 cboUserList 下拉列表中对用户可见。
有什么想法吗?
您可以将密钥包装在 class 中,例如
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;
确保稍后销毁这些对象
一个简单的跨平台解决方案是使用单独的 TStringList
来保存 key
,然后在 ComboBox 中显示 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;
我想使用枚举器来填充具有 Key/Value 对的组合框。重要的是我对用户隐藏密钥并仅显示值。在 selecting 上,我想捕获与 selected 值关联的密钥。
代码看起来与此类似。
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['key'] 应该在值的用户 select 上捕获 currentObj.S['value'] 在 cboUserList 下拉列表中对用户可见。
有什么想法吗?
您可以将密钥包装在 class 中,例如
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;
确保稍后销毁这些对象
一个简单的跨平台解决方案是使用单独的 TStringList
来保存 key
,然后在 ComboBox 中显示 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;