我想出了这个解决方案。如果您需要对列表进行排序(我没有),它将不起作用。 TLama提供的解决方案更好。请投票他的答案。
TYPE
TDualComboBox = class(TComboBox)
private
FDItems: TStrings; { Strings are separated with ##}
function getDItems: TStrings;
procedure setDItems (const DualItems: TStrings);
protected
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure AddDualItem(const DualItem: String);
function SelectedDualItem: string;
property DualItems: TStrings read getDItems write setDItems; { Strings are separated with ##}
published
end;
procedure Register;
IMPLEMENTATION
Constructor TDualComboBox.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
FDItems:= TStringList.Create;
end;
destructor TDualComboBox.Destroy;
begin
FreeAndNil(FDItems);
inherited;
end;
procedure TDualComboBox.AddDualItem(CONST DualItem: String);
VAR
sField, sValue: string;
begin
SplitString(DualItem, '##', sField, sValue);
Items .Add(sField);
FDItems.Add(sValue);
end;
function TDualComboBox.SelectedDualItem: string;
begin
if ItemIndex < 0
then Result:= ''
else Result:= FDItems[ItemIndex];
end;
这是测试程序:
procedure TForm5.FormCreate(Sender: TObject);
begin
Box:= TcComboBox.Create(Self);
Box.Parent:= Self;
Box.Top := 200;
Box.Left:= 200;
Box.OnChange:= ComboChange;
Button1Click(Sender);
end;
procedure TForm5.Button1Click(Sender: TObject);
begin
Box.AddDualItem('User nice text##usr_bkg_text');
end;
procedure TForm5.ComboChange(Sender: TObject);
begin
lblInfo.Caption:= Box.SelectedDualItem;
end;
只要保持一个的TStringList的地方,并使用组合框项目索引的索引? – whosrdaddy
或者创建一个“命令”对象并将其分配给组合框的Objects [Index]属性? – whosrdaddy
@ whosrdaddy-我想我喜欢第二个想法 – Ampere