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

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

问题描述

如何创建一个带有两列的 TComboBox 并隐藏其中一列,以便在其中保留 id 值和实际项目?然后如何以编程方式获取该 id 值?

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?

推荐答案

这里不需要两列.

您可以利用 TComboBox.Items(就像 Delphi 中的许多其他东西,如 TStringListTMemo.Lines、和 TListBox.Items) 继承自 TStrings,它同时具有 StringsObjects 属性.Objects 存储任何大小为 TObject 的东西,它是一个指针.

You can take advantage of the fact that TComboBox.Items (like many other things in Delp 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.

这意味着您可以通过简单地在添加时将其类型转换为 TObject 并在检索时将其类型转换回 Integer 来存储整数值.

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;

检索值:

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;

请注意,由于Objects 属性实际上是用来存储对象,这为您提供了很大的灵活性.这是一个示例(故意非常简单),将客户的联系信息存储在关联的对象实例中,并在选择列表框中的项目时将其显示在标签中.

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天全站免登陆