另一种解决方案是使用 TComboBox 而不是 TDBLookupComboBox。使用 TDictionary 定义简单的内存查找。
type
TMyForm = class(TForm)
MyComboBox: TComboBox;
MyDataset: TSimpleDataSet;
procedure MyComboBoxChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
ComboLookup: TDictionary<string, Char>;
end;
implementation
{$R *.dfm}
procedure TMyForm.FormCreate(Sender: TObject);
var
Key: string;
begin
ComboLookup := TDictionary<string, Char>.Create;
ComboLookup.Add('Drivers License', 'A');
ComboLookup.Add('Passport', 'B');
ComboLookup.Add('Library Card', 'C');
for Key in ComboLookup.Keys do
begin
MyComboBox.Items.Add(Key);
end;
end;
procedure TMyForm.MyComboBoxChange(Sender: TObject);
begin
// This may be wrong didn't bother to look
//up the correct way to change a field's value in code.
MyDataset.Fields.FieldByName('IDCard').AsString := ComboLookup[MyComboBox.Text];
end;
您可以使用 TComboBox.Items.AddObject 而不是单独的查找表,但您必须创建一个包装类来将 char 存储为 TObject 或使用 Chr 将其转换为整数然后将其转换为 TObject 但以上是在我看来更简单。