2015-06-19 40 views
0

我有一个TCombobox,我向用户展示了一些项目。但是我向用户展示的项目文本与我需要的文本不同。如何在TComboBox中添加'hidden'项目?

例如,组合框项目和文字,我实际上需要的是:

Entry start -> cmd_estart 
Entry End  -> cmd_eend 

的“命令”我需要当用户点击第一项“cmd_estart”。有一种方法可以将第二个项目列表放入组合框中?

换句话说,我需要项目的另一个列表,与已存在的项目列表的原始列表'并行'。

我希望有这样的东西已经作出:)所以,如果你知道这样的控制,请发布一个链接。


注:这不是How to create a combobox with two columns (one hidden) in Delphi 7?重复,因为这个问题询问有关如何在组合框显示两列。所提供的解决方案不如此处提供的解决方案(TLama)。

+0

只要保持一个的TStringList的地方,并使用组合框项目索引的索引? – whosrdaddy

+1

或者创建一个“命令”对象并将其分配给组合框的Objects [Index]属性? – whosrdaddy

+0

@ whosrdaddy-我想我喜欢第二个想法 – Ampere

回答

0

我想出了这个解决方案。如果您需要对列表进行排序(我没有),它将不起作用。 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; 
+2

字符串列表具有用于任务名称值对的字符串列表,因此不需要为此具有单独的字符串列表。您可以显示值并获取名称(具有像'cmd_estart = Entry start'这样的项目)。 – TLama

+3

这就是你使用所有者绘图的地方。你可以设置'Style',例如到'csOwnerDrawFixed'并在'OnDrawItem'事件处理程序中打印出项目的值。有些东西['like this'](http://pastebin.com/8A4e4JKb)。 – TLama

+0

名称值对由第一次出现的“NameValueSeparator”字符分析,您可以根据需要进行更改。默认情况下它是'='char,所以命令名称(名称部分)不能包含此char。价值部分可以。 – TLama