//ComboBox.AddObjectprocedure TForm1.FormCreate(Sender: TObject); begin with QryCustType do begin Close; sql.Clear; sql.Text:='select * from CUSTTYPE'; open; while not Eof do begin ComboBox1.Items.AddObject(fieldbyname('name').AsString,Pointer(fieldbyname('custtypeid').AsInteger)); next; end; end; end; 显示Value procedure TForm1.Button2Click(Sender: TObject); var A_Value:integer; begin A_Value:=integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]); Showmessage(inttostr(A_Value)); end;
在窗体创建时加入如下代码: with ADOQuery1 do begin Close; SQL.Clear: SQL.Add('select id,name from table_name'); Open; First; While not Eof do begin Combobox1.Items.Add(FieldByName('id').AsString); Combobox1.Values.Add(FieldByName('name').AsString); Next; end; end; 取对应的id值: id:= Combobox1.Values.Strings[Combobox1.ItemIndex];
begin
with QryCustType do
begin
Close;
sql.Clear;
sql.Text:='select * from CUSTTYPE';
open;
while not Eof do
begin
ComboBox1.Items.AddObject(fieldbyname('name').AsString,Pointer(fieldbyname('custtypeid').AsInteger));
next;
end;
end;
end; 显示Value
procedure TForm1.Button2Click(Sender: TObject);
var
A_Value:integer;
begin
A_Value:=integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
Showmessage(inttostr(A_Value));
end;
with ADOQuery1 do
begin
Close;
SQL.Clear:
SQL.Add('select id,name from table_name');
Open;
First;
While not Eof do
begin
Combobox1.Items.Add(FieldByName('id').AsString);
Combobox1.Values.Add(FieldByName('name').AsString);
Next;
end;
end;
取对应的id值:
id:= Combobox1.Values.Strings[Combobox1.ItemIndex];
Combobox1.Items.Add(FieldByName('id').AsString);
Combobox1.Values.Add(FieldByName('name').AsString);
这两句反了,应为:
Combobox1.Items.Add(FieldByName('name').AsString);
Combobox1.Values.Add(FieldByName('id').AsString);
这样 id:= Combobox1.Values.Strings[Combobox1.ItemIndex];
id 取的值才对