+ -
当前位置:首页 → 问答吧 → delphi combobox自动通过模糊查询填充下拉框

delphi combobox自动通过模糊查询填充下拉框

时间:2011-09-17

来源:互联网

想实现功能数据比如是2个 南方机械制造公司 南通责任有限公司
在combobox中输入“南”的时候自动下拉框自动下拉 把包含“南”关键字的数据都找到填充
下面是我自己的代码 发现下拉框能找到2个数据 但是默认选中排列第一个的数据 并且下拉框中数据不能用光标单击选择 只能用键盘确定键选中第一个数据 为了自动下拉的实现我吧autodropdown 属性设置true  
请高手帮我改改 谢谢了
if(combobox3.text<>'') then begin
  with ADOQuery2 do
  begin
  close;
  SQL.Clear;
  SQL.Add('Select * FROM tb_company where CompanyName like :p and companytype like :p1');
  Parameters.ParamByName('p').Value:='%'+trim(combobox3.text)+'%';
  Parameters.ParamByName('p1').Value:='顾客';
  open;
  end;
  combobox3.items.text:='';
  ComboBox3.SelStart:= Length(ComboBox3.Text);
 while not adoquery2.Eof do
  begin

  ComboBox3.Items.Add(adoquery2.Fieldbyname('CompanyName').AsString);
  adoquery2.Next;
  end;
  end;

作者: a381391536   发布时间: 2011-09-17

得自己开发控件,或是用ButtonEdit+LookUpCombobox结合做啊。

作者: HelpMeNow   发布时间: 2011-10-15

自己在Onchange事件中,加入搜索代码即可。

作者: lyhoo163   发布时间: 2011-10-15

这种下拉的实现多了去了!用两个控件结合使用,应该会更好实现。我用Edit和ListBox实现过。
在Eidt的Change事件中,如下代码
procedure TFrm_EditInfo.Ed_CarCodeChange(Sender: TObject);
var
  CarCode: string;
begin
  if Trim(Ed_CarCode.Text) <> '' then
  begin
  //取查询的条件
  CarCode := Trim(Ed_CarCode.Text);
  //这是一个公用函数,用来将查询的数据显示到ListBox中。
  DM.BindListBox(Lst_CarInfo, CarCode);
  //如果ListBox中有数据,则显示ListBox,否则就隐藏
  if Lst_CarInfo.Items.Count > 0 then
  begin
  Lst_CarInfo.Left := Ed_CarCode.Left;
  Lst_CarInfo.Top := Ed_CarCode.Top + Ed_CarCode.Height;
  Lst_CarInfo.Width := Ed_CarCode.Width;
  Lst_CarInfo.Visible := True;
  end
  else
  Lst_CarInfo.Visible := false;
  end
  else
  begin
  //如果没有输入条件,则清空。
  Lst_CarInfo.Visible := false;
  Lst_CarInfo.Items.Clear;
  end;
end;
为了使用键盘的上下键进行选择,所以在Edit的KeyDown事件中写如下代码
//判断是否按了下键,是就把光标移动到ListBox中。
if Key = 40 then
  begin
  if (Lst_CarInfo.Visible) then
  begin
  Lst_CarInfo.SetFocus;
  Lst_CarInfo.Selected[0] := true;
  end;

  end; 
实现鼠标选择更简单,在ListBox的双击事件中写如下代码
if Lst_CarInfo.ItemIndex <> -1 then
  begin
  Ed_CarCode.text := Lst_CarInfo.Items.Strings[Lst_CarInfo.ItemIndex] 
  end;
上面这句在ListBox的回车事件中也可以实现。

作者: nm_wyh   发布时间: 2011-10-15

补充一下,这种下拉实现主要是为了方便。最好是使用汉字的首拼或者编号一类的进行过滤。如果输入汉字过滤。只能说只实现了一少半的方便。

作者: nm_wyh   发布时间: 2011-10-15