是这样: 有一个类:
TStudent = class(TObject)
ID: integer;
Name: string;
public
constructor create;
end; constructor TStudent.create;
begin
id := 0;
name := '';
end;
在程序里把student表中的数据加到一个combobox里面:
procedure TForm1.Button3Click(Sender: TObject);
var AStudent: TStudent;
AQuery: TAdoQuery;
idx: integer;
begin
AQuery := TAdoQuery.creat(Self);
with AQuery do
begin
connection := dm.adoconnection;
sql.text := 'select id,name from student';
open;
while not eof do
begin
idx := Combobox1.items.add(FieldByName('name').asstring);
AStudent := TStudent.create;
AStudent.id := FieldByName('id').AsInteger;
AStudent.name := FieldByName('name').asstring;
ComboBox1.Items.Objects[index]:= TObject(AStudent);
next;
end;
end;
end;
疑问如下:
idx := Combobox1.items.add(FieldByName('name').asstring);
AStudent := TStudent.create;
AStudent.id := FieldByName('id').AsInteger;
AStudent.name := FieldByName('name').asstring;
ComboBox1.Items.Objects[index]:= TObject(AStudent);
以上这几句的意思是什么?
以上那样做有什么好处?
最后对对象的访问:
id:=TStudent(Combobox1.Items.Objects[combobox1.ItemIndex]).ID;Combobox1.Items、Combobox1.Items.Objects 和AStudent之间的关系是什么?
TStudent = class(TObject)
ID: integer;
Name: string;
public
constructor create;
end; constructor TStudent.create;
begin
id := 0;
name := '';
end;
在程序里把student表中的数据加到一个combobox里面:
procedure TForm1.Button3Click(Sender: TObject);
var AStudent: TStudent;
AQuery: TAdoQuery;
idx: integer;
begin
AQuery := TAdoQuery.creat(Self);
with AQuery do
begin
connection := dm.adoconnection;
sql.text := 'select id,name from student';
open;
while not eof do
begin
idx := Combobox1.items.add(FieldByName('name').asstring);
AStudent := TStudent.create;
AStudent.id := FieldByName('id').AsInteger;
AStudent.name := FieldByName('name').asstring;
ComboBox1.Items.Objects[index]:= TObject(AStudent);
next;
end;
end;
end;
疑问如下:
idx := Combobox1.items.add(FieldByName('name').asstring);
AStudent := TStudent.create;
AStudent.id := FieldByName('id').AsInteger;
AStudent.name := FieldByName('name').asstring;
ComboBox1.Items.Objects[index]:= TObject(AStudent);
以上这几句的意思是什么?
以上那样做有什么好处?
最后对对象的访问:
id:=TStudent(Combobox1.Items.Objects[combobox1.ItemIndex]).ID;Combobox1.Items、Combobox1.Items.Objects 和AStudent之间的关系是什么?
AStudent.id := FieldByName('id').AsInteger; //为对象属性设置值
AStudent.name := FieldByName('name').asstring;//为对象属性设置值
ComboBox1.Items.Objects[index]:= TObject(AStudent);看看TCombox的帮助和刘艺写的delphi面向对象编程思想就懂了.
combobox.items --- 存放了你的姓名,为了找你时方便
combobox.vitems.objects ----存放了你的躯体