Mik*_*ail 7 delphi combobox delphi-7
如何创建一个TComboBox,其中有两列隐藏了其中一列,以便它可以保存id值以及其中的实际项目?然后如何以编程方式获得该id值?
Ken*_*ite 10
这里不需要两列.
你可以利用这一事实,这种优势TComboBox.Items(像德尔福许多其他的东西,比如TStringList,TMemo.Lines和TListBox.Items)从下降TStrings,其中既有Strings和Objects属性.Objects存储大小为a的任何东西TObject,这是一个指针.
这意味着您可以通过在添加它时将其类型转换为TObject来存储整数值,并在检索时将其类型转换回Integer.
这样的事情应该有效:
procedure TForm1.FormCreate(Snder: TObject);
var
i: Integer;
sItem: String;
begin
for i := 0 to 9 do
begin
sItem := Format('Item %d', [i]);
ComboBox1.Items.AddObject(sItem, TObject(i));
end;
end;
Run Code Online (Sandbox Code Playgroud)
要检索值:
procedure TForm1.ComboBox1Click(Sender: TObject);
var
Idx: Integer;
Value: Integer;
begin
Idx := ComboBox1.ItemIndex;
if Idx <> -1 then
begin
Value := Integer(ComboBox1.Items.Objects[Idx]);
// Do something with value you retrieved
end;
end;
Run Code Online (Sandbox Code Playgroud)
请注意,由于Objects属性实际上是用于存储对象,因此这为您提供了很大的灵活性.这是一个示例(故意非常简单)将客户的联系信息存储在关联的对象实例中,并在选择列表框中的项目时将其显示在标签中.
unit Unit1;
interface
uses
Windows, Messages, Variants, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TCustomer=class
private
FContact: string;
FPhone: string;
public
constructor CreateCustomer(const AContact, APhone: string);
property Contact: string read FContact write FContact;
property Phone: string read FPhone write FPhone;
end;
TForm1 = class(TForm)
ListBox1: TListBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
lblContact: TLabel;
lblPhone: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TCustomer }
constructor TCustomer.CreateCustomer(const AContact, APhone: string);
begin
inherited Create;
FContact := AContact;
FPhone := APhone;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
i: Integer;
begin
for i := 0 to ListBox1.Items.Count - 1 do
ListBox1.Items.Objects[i].Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
lblContact.Caption := '';
lblPhone.Caption := '';
// Create some customers. Of course in the real world you'd load this
// from some persistent source instead of hard-coding them here.
ListBox1.Items.AddObject('N Company', TCustomer.CreateCustomer('Nancy', '555-3333'));
ListBox1.Items.AddObject('B Company', TCustomer.CreateCustomer('Brad', '555-1212'));
ListBox1.Items.AddObject('A Company', TCustomer.CreateCustomer('Angie', '555-2345'));
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
Cust: TCustomer;
begin
if ListBox1.ItemIndex <> -1 then
begin
Cust := TCustomer(ListBox1.Items.Objects[ListBox1.ItemIndex]);
lblContact.Caption := Cust.Contact;
lblPhone.Caption := Cust.Phone;
end;
end;
end.
Run Code Online (Sandbox Code Playgroud)
ComboBox控件不支持列,无论如何都不需要隐藏列来完成所需的操作.
该TComboBox.Items物业是一个TStrings后代.它可以同时保存字符串值和关联的用户定义数据值,但用户只能在UI中看到字符串值.使用此Items.AddObject()方法将字符串+ ID值添加到列表中,然后使用该Items.Objects[]属性在需要时检索ID值.
或者,您可以将ID值存储在与ComboBox具有相同数量元素的单独数组中,然后使用ComboBox项索引来访问数组值.如果您需要存储值-1,这一点尤其重要,因为由于getter方法的实现方式,该特定值无法从Objects[]属性中检索TComboBox,如Rob所说.