我的代码是: procedure TForm15.DBEdit1Change(Sender: TObject); begin dw.Style:=csDropDown; combobox1.Style:=csDropDown; combobox2.Style:=csDropDown; dw.Text:=''; zg_num.Text:=''; zg_xm.Text:=''; age.Text:=''; Combobox1.Text:=''; Combobox2.Text:=''; If DBEdit1.Text= '' Then Exit else begin ADOQuery4.Close; ADOQuery4.SQL.Clear; ADOQuery4.SQL.Add('select * from gh_ghxx where dw_zg_num='''+DBEdit1.Text+''' '); ADOQuery4.Open; dw.Text:=ADOQuery4.fieldbyname('dw_name').AsString; zg_num.Text:=ADOQuery4.fieldbyname('dw_zg_num').asstring; zg_xm.Text:=ADOQuery4.fieldbyname('dw_zg_name').asstring; age.Text:=ADOQuery4.fieldbyname('dw_zg_age').asstring; Combobox1.Text:=ADOQuery4.fieldbyname('dw_zg_xb').asstring; Combobox2.Text:=AdoQuery4.fieldbyname('dw_zg_xl').AsString; DateTimePicker1.DateTime:=strtodate(ADOQuery4.fieldbyname('dw_zg_time').asstring); end; dw.Style:=csDropDownlist; combobox1.Style:=csDropDownlist; combobox2.Style:=csDropDownlist;end;
如果设置为csDropDownList时,你再赋值时要动态的符值,用combobox.text := 你的值,这样是不行 得这样var i: integer; begin for i:= 0 to Combobox1.Items.Count-1 do begin if combobox1.Items[i] = 你的值 then begin combobox1.ItemIndex := i; break; end; end; end;
但是从数据库中查询的结果就只有一个啊,比如数据库中该combobox存放的性别,它只有男和女,显示的时候也就只能显示一个值。
如果不将style属性设为:csDropDownlist
,到可以显示值“男”,那样combobox就不是只读了。
不知道明白没有?
procedure TForm15.DBEdit1Change(Sender: TObject);
begin
dw.Style:=csDropDown;
combobox1.Style:=csDropDown;
combobox2.Style:=csDropDown;
dw.Text:='';
zg_num.Text:='';
zg_xm.Text:='';
age.Text:='';
Combobox1.Text:='';
Combobox2.Text:='';
If DBEdit1.Text= '' Then Exit
else begin
ADOQuery4.Close;
ADOQuery4.SQL.Clear;
ADOQuery4.SQL.Add('select * from gh_ghxx where dw_zg_num='''+DBEdit1.Text+''' ');
ADOQuery4.Open;
dw.Text:=ADOQuery4.fieldbyname('dw_name').AsString;
zg_num.Text:=ADOQuery4.fieldbyname('dw_zg_num').asstring;
zg_xm.Text:=ADOQuery4.fieldbyname('dw_zg_name').asstring;
age.Text:=ADOQuery4.fieldbyname('dw_zg_age').asstring;
Combobox1.Text:=ADOQuery4.fieldbyname('dw_zg_xb').asstring;
Combobox2.Text:=AdoQuery4.fieldbyname('dw_zg_xl').AsString;
DateTimePicker1.DateTime:=strtodate(ADOQuery4.fieldbyname('dw_zg_time').asstring);
end;
dw.Style:=csDropDownlist;
combobox1.Style:=csDropDownlist;
combobox2.Style:=csDropDownlist;end;
得这样var
i: integer;
begin
for i:= 0 to Combobox1.Items.Count-1 do
begin
if combobox1.Items[i] = 你的值 then
begin
combobox1.ItemIndex := i;
break;
end;
end;
end;
csDropDownlist时,combobox.text属性是不能直接赋值的,你得指定你的列表项作为combobox.text的值
Itemindex=0 代表把列表框的第一项作为combobox.text
Itemindex=1 代表把列表框的第二项作为combobox.text
combobox1.Style:=csDropDownList; //设置只读。
combobox1.ItemIndex:=0; //使第一项选中。
男:
cbbSex.ItemIndex := cbbSex.Items.IndexOf('男');
女:
cbbSex.ItemIndex := cbbSex.Items.IndexOf('女');