iecombobox,有现成控件的 不过这样也可以(转贴) procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char); var Found: boolean; i, SelSt: Integer; TmpStr: string; begin { first, process the keystroke to obtain the current string } { This code requires all items in list to be uppercase} with (Sender as TComboBox) do begin SelSt := SelStart; if (Key = Chr(vk_Back)) and (SelLength <> 0) then TmpStr := Copy(Text, 1, SelStart) + Copy(Text, SelLength + SelStart + 1, 255) else if Key = Chr(vk_Back) then {SelLength = 0} TmpStr := Copy(Text, 1, SelStart - 1) + Copy(Text, SelStart + 1, 255) else {Key in ['A'..'Z', etc]} TmpStr := Copy(Text, 1, SelStart) + Key + Copy(Text, SelLength + SelStart + 1, 255); if TmpStr = ' then Exit; { update SelSt to the current insertion point } if (Key = Chr(vk_Back)) and (SelSt > 0) then Dec(SelSt) else if Key <> Chr(vk_Back) then Inc(SelSt); Key := #0; { indicate that key was handled } if SelSt = 0 then begin Text := '; Exit; end; {Now that TmpStr is the currently typed string, see if we can locate a match } Found := False; for i := 1 to Items.Count do if UpperCase(Copy(Items[i - 1], 1, Length(TmpStr))) = Uppercase(TmpStr) then //all from help,Yanlei modify Uppercase begin Text := Items[i - 1]; { update to the match that was found } ItemIndex := i - 1; Found := True; Break; end; if Found then { select the untyped end of the string } begin SelStart := SelSt; SelLength := Length(Text) - SelSt; ComboBox1.AutoDropDown := true;//yanlei add end else Beep; end; end;
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) ComboBox1: TComboBox; ListBox1: TListBox; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure ComboBox1Change(Sender: TObject); private { Private declarations } FStringList:TStringList; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject); begin FStringList:=TStringList.Create; FStringList.AddStrings(ComboBox1.Items); ComboBox1.AutoComplete:=false; end;procedure TForm1.FormDestroy(Sender: TObject); begin FStringList.Clear; FStringList.Free; end;procedure TForm1.ComboBox1Change(Sender: TObject); var i:integer; begin if (Sender as TComboBox).Text<>'' then begin (Sender as TComboBox).Items.Clear; ListBox1.Items.Clear; //可去掉 ,测试用的 For i:=0 to FStringList.Count-1 do if pos((Sender as TComboBox).Text,FStringList.Strings[i])=1 then begin (Sender as TComboBox).Items.Add(FStringList.Strings[i]); ListBox1.Items.Add(FStringList.Strings[i]); //可去掉,测试用的 end; end else (Sender as TComboBox).Items.AddStrings(FStringList); (Sender as TComboBox).SelStart:=1024; end;end.
一个sql语句就可以搞定了。select × from tabel where 字段 like combobox.text 写在keypress或者keyup里面都可以!
不过这样也可以(转贴)
procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
var
Found: boolean;
i, SelSt: Integer;
TmpStr: string;
begin
{ first, process the keystroke to obtain the current string }
{ This code requires all items in list to be uppercase}
with (Sender as TComboBox) do
begin
SelSt := SelStart;
if (Key = Chr(vk_Back)) and (SelLength <> 0) then
TmpStr := Copy(Text, 1, SelStart) + Copy(Text, SelLength + SelStart + 1, 255)
else if Key = Chr(vk_Back) then {SelLength = 0}
TmpStr := Copy(Text, 1, SelStart - 1) + Copy(Text, SelStart + 1, 255)
else {Key in ['A'..'Z', etc]}
TmpStr := Copy(Text, 1, SelStart) + Key + Copy(Text, SelLength + SelStart + 1, 255);
if TmpStr = ' then Exit;
{ update SelSt to the current insertion point }
if (Key = Chr(vk_Back)) and (SelSt > 0) then Dec(SelSt)
else if Key <> Chr(vk_Back) then Inc(SelSt);
Key := #0; { indicate that key was handled }
if SelSt = 0 then
begin
Text := ';
Exit;
end;
{Now that TmpStr is the currently typed string, see if we can locate a match }
Found := False;
for i := 1 to Items.Count do
if UpperCase(Copy(Items[i - 1], 1, Length(TmpStr))) = Uppercase(TmpStr) then
//all from help,Yanlei modify Uppercase
begin
Text := Items[i - 1]; { update to the match that was found }
ItemIndex := i - 1;
Found := True;
Break;
end;
if Found then { select the untyped end of the string }
begin
SelStart := SelSt;
SelLength := Length(Text) - SelSt;
ComboBox1.AutoDropDown := true;//yanlei add
end
else Beep;
end;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ComboBox1: TComboBox;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
FStringList:TStringList;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
FStringList:=TStringList.Create;
FStringList.AddStrings(ComboBox1.Items);
ComboBox1.AutoComplete:=false;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
FStringList.Clear;
FStringList.Free;
end;procedure TForm1.ComboBox1Change(Sender: TObject);
var
i:integer;
begin
if (Sender as TComboBox).Text<>'' then
begin
(Sender as TComboBox).Items.Clear;
ListBox1.Items.Clear; //可去掉 ,测试用的
For i:=0 to FStringList.Count-1 do
if pos((Sender as TComboBox).Text,FStringList.Strings[i])=1 then
begin
(Sender as TComboBox).Items.Add(FStringList.Strings[i]);
ListBox1.Items.Add(FStringList.Strings[i]); //可去掉,测试用的
end;
end
else
(Sender as TComboBox).Items.AddStrings(FStringList);
(Sender as TComboBox).SelStart:=1024;
end;end.
写在keypress或者keyup里面都可以!