可能没办法作到,因为combobox的onchenge属性是在选择的时候,而不是在输入的时候的,其他想不到什么好的事件,给个提示: with adoquery1 do begin close; sql.clear; sql.text := 'select * from 表 where 字段 like ''%'+combobox.text+'%'''; open; end; with adoquery1 do begin close; sql.clear; sql.text := 'select * from 表 where 字段= '''+combobox.text+''''; open; end;
写成这样,但每次在combobox中输入数据,它都是从左输入,不知如何解决 procedure Tmailform.RzComboBox1KeyPress(Sender: TObject; var Key: Char); var i:integer; id1,name1:string; begin SendMessage(rzComboBox1.handle, CB_SHOWDROPDOWN, Integer(false), 0); i:=length(rzcombobox1.Text); adoquery1.First; rzcombobox1.Items.Clear; while not adoquery1.Eof do begin id1:=adoquery1.FieldValues['id']; name1:=adoquery1.FieldValues['name']; if (copy(id1,1,i)=rzcombobox1.Text) or (copy(name1,1,i)=rzcombobox1.Text) then rzcombobox1.Add(name1); adoquery1.Next; end; sendmessage(rzcombobox1.Handle,cb_showdropdown,integer(true),0); end;
with adoquery1 do
begin
close;
sql.clear;
sql.text := 'select * from 表 where 字段 like ''%'+combobox.text+'%''';
open;
end;
with adoquery1 do
begin
close;
sql.clear;
sql.text := 'select * from 表 where 字段= '''+combobox.text+'''';
open;
end;
你可以在onkeypress或者是onkeydown事件里写查找代码看看行不?
procedure Tmailform.RzComboBox1KeyPress(Sender: TObject; var Key: Char);
var
i:integer;
id1,name1:string;
begin
SendMessage(rzComboBox1.handle, CB_SHOWDROPDOWN, Integer(false), 0);
i:=length(rzcombobox1.Text);
adoquery1.First;
rzcombobox1.Items.Clear;
while not adoquery1.Eof do
begin
id1:=adoquery1.FieldValues['id'];
name1:=adoquery1.FieldValues['name'];
if (copy(id1,1,i)=rzcombobox1.Text) or (copy(name1,1,i)=rzcombobox1.Text) then
rzcombobox1.Add(name1);
adoquery1.Next;
end;
sendmessage(rzcombobox1.Handle,cb_showdropdown,integer(true),0);
end;
2,在ComboBox的OnChange事件中,將ComboBox所有的數據刪除,然後從Query控件中循環比較
前面幾個字符與ComboBox的字符是否都相同的,如果都相同就加入到ComboBox中。