关于ComboBox.Items.AddObject用法
之前在代码中增加了
ComboBox.Items.AddObject('一班','4');
ComboBox.Items.AddObject('二班','5');
ComboBox.Items.AddObject('三班','6');
unit Unit13;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
PClz = ^TClz;
TClz = record
Name : string;
ID : integer;
end;
TForm13 = class(TForm)
cbb1: TComboBox;
procedure FormCreate(Sender: TObject);
procedure cbb1Change(Sender: TObject);
private
{ Private declarations }
Clzs : array of TClz; //Clzs要保证在TForm13中始终有效, 所以在此定义
public
{ Public declarations }
end;
var
Form13: TForm13;
implementation
{$R *.dfm}
procedure TForm13.cbb1Change(Sender: TObject);
begin
with cbb1.Items do
begin
ShowMessage(IntToStr(PClz(Objects[IndexOf(cbb1.Text)]).ID));
end;
end;
procedure TForm13.FormCreate(Sender: TObject);
begin
SetLength(Clzs, 3);
Clzs[0].Name := '一班';
Clzs[0].ID := 4;
Clzs[1].Name := '二班';
Clzs[1].ID := 5;
Clzs[2].Name := '三班';
Clzs[2].ID := 6;
cbb1.Clear;
cbb1.Items.AddObject(Clzs[0].Name, @Clzs[0]);
cbb1.Items.AddObject(Clzs[1].Name, @Clzs[1]);
cbb1.Items.AddObject(Clzs[2].Name, @Clzs[2]);
end;
end.