如何在Delphi 7中创建一个带有两列(一个隐藏)的组合框? [英] How to create a combobox with two columns (one hidden) in Delphi 7?

查看:227
本文介绍了如何在Delphi 7中创建一个带有两列(一个隐藏)的组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个 TComboBox ,它有两列,其中一列隐藏,以便它可以保持一个id值以及其中的实际项目?然后如何以编程方式获取那个id值?

解决方案

这里不需要两列。



你可以利用 TComboBox.Items (像Delphi中很多其他东西一样,例如 TStringList TMemo.Lines TListBox.Items )从 TStrings ,它同时具有 Strings Objects 属性。 对象存储任何大小为 TObject 的任何东西,这是一个指针。



这意味着您可以在添加整数值时将其整型转换为 TObject ,并在检索时将其整型回整数 / p>

这样应该可以工作:

  procedure TForm1.FormCreate :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;

要检索值:

  procedure TForm1.ComboBox1Click(Sender:TObject); 
var
Idx:Integer;
值:Integer;
begin
Idx:= ComboBox1.ItemIndex;
if Idx<> -1 then
begin
Value:= Integer(ComboBox1.Items.Objects [Idx]);
//用你检索的值做一些事情
end;
end;

请注意,由于对象属性实际上是用来存储对象,这给了你很大的灵活性。下面是一个示例(有意非常简单):当选择列表框中的项目时,将客户的联系信息存储在关联对象实例中并将其显示在标签中。

  unit Unit1; 

接口

使用
Windows,消息,变量,类,图形,控件,
窗体,对话框,StdCtrls;

type
TCustomer = class
private
FContact:string;
FPhone:string;
public
constructor CreateCustomer(const AContact,APhone:string);
property联系人:string读取FContact写入FContact;
property电话:字符串读FPhone写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
{私人声明}
public
{公共声明}
end;

var
Form1:TForm1;

实现

{$ R * .dfm}

{TCustomer}

构造函数TCustomer.CreateCustomer(const AContact ,APhone:string);
begin
inherited创建;
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:='';

//创建一些客户。当然,在现实世界中,你可以从一些持久化源代码中加载
//,而不是在这里硬编码。
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。


How to create a TComboBox with two columns that has one of its columns hidden so that it can keep an id value along with the actual item in it? And then how to get to that id value programmatically?

解决方案

There's no need for two columns here.

You can take advantage of the fact that TComboBox.Items (like many other things in Delphi, like TStringList, TMemo.Lines, and TListBox.Items) descends from TStrings, which has both the Strings and Objects properties. Objects stores anything the size of a TObject, which is a pointer.

This means you can store your integer value by simply typecasting it to a TObject when adding it, and typecasting it back to an Integer when retrieving it.

Something like this should work:

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;

To retrieve the value:

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;

Note that, since the Objects property is actually meant to store objects, this gives you a lot of flexibility. Here's an example (intentionally very trivial) of storing a customer's contact information in an associated object instance and displaying it in labels when an item from a listbox is selected.

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.

这篇关于如何在Delphi 7中创建一个带有两列(一个隐藏)的组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆