我的程序如下:
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
with adoqueryjbqk do
  begin
  close;
  Sql.Clear;
  Sql.Add('select sxmc from mingxi where sxlbid=003 and sxpy like '''+combobox1.text+'%''');
  Open;
  combobox1.items.clear;
  First;
  while not eof do
  begin
    combobox1.items.add(fieldbyname('sxmc').asstring);
    Next;
  end;
end;
end;
我要完成的功能是用户在combobx.text中输入的内容为查询条件,combobx.items的内容是查询的结果,然后用户便可在下拉菜单中选择要选的内容了.
问题是:在items中显示出了从库中查询的内容,但当我想选中一个内容时,combobox又空了,我想是combobox1.items.clear;这一句的问题,可去了这一句,每次选的内容就又都还在下拉菜单中,不知为什么?请高手赐教!

解决方案 »

  1.   

    建议你把事件写在KeyPress中,按回车就查询
    procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
    begin
      if Key=#13 then
      begin
        with adoqueryjbqk do
          begin
          close;
          Sql.Clear;
          Sql.Add('select sxmc from mingxi where sxlbid=003 and sxpy like '''+combobox1.text+'%''');
          Open;
          combobox1.items.clear;
          First;
          while not eof do
          begin
            combobox1.items.add(fieldbyname('sxmc').asstring);
            Next;
          end;
        end;
      end;
    end;
      

  2.   

    你不应该把这个事件写到TForm1.ComboBox1Change这个事件中
    换个其它地方写
      

  3.   

    你写OnChange事件中,想选中一个内容时,又触发了Change事件,自然有问题。
    上边的我给改成按回车查询了
    事件写在KeyPress中
      

  4.   

    这事我碰着过,是你的combobox1.items.clear;的位置不对