1. 判断输入的字符串是否与combobox的items中的相同
2. 如相同, 设置itemindex
3. 设置DroppedDown=true
如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;type
TForm1 = class(TForm)
ComboBox1: TComboBox;
procedure ComboBox1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ComboBox1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
i:integer;
NewIndex:integer;
begin
NewIndex:=-1;
for i:=0 to combobox1.items.count-1 do
if combobox1.items[i]=combobox1.text then
begin
NewIndex:=i;
break;
end;
if NewIndex<>-1 then
begin
combobox1.ItemIndex:=i;
combobox1.DroppedDown:=true;
end;
end;
end.
2. 如相同, 设置itemindex
3. 设置DroppedDown=true
如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;type
TForm1 = class(TForm)
ComboBox1: TComboBox;
procedure ComboBox1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ComboBox1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
i:integer;
NewIndex:integer;
begin
NewIndex:=-1;
for i:=0 to combobox1.items.count-1 do
if combobox1.items[i]=combobox1.text then
begin
NewIndex:=i;
break;
end;
if NewIndex<>-1 then
begin
combobox1.ItemIndex:=i;
combobox1.DroppedDown:=true;
end;
end;
end.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TCompletingComboBox = class(TComboBox)
private
FTextCompletion: Boolean;
function GetTextCompletion: Boolean;
procedure SetTextCompletion(const Value: Boolean);
protected
// override the WndProc() so that we can trap KeyUp events.
procedure ComboWndProc(var Message: TMessage; ComboWnd: HWnd;
ComboProc: Pointer); override;
public
{ Public declarations }
published
property TextCompletion: Boolean read GetTextCompletion write SetTextCompletion;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Standard', [TCompletingComboBox]);
end;{ TCompletingComboBox }function TCompletingComboBox.GetTextCompletion: Boolean;
begin
Result := fTextCompletion;
end;procedure TCompletingComboBox.SetTextCompletion(const Value: Boolean);
begin
fTextCompletion := Value;
end;procedure TCompletingComboBox.ComboWndProc(var Message: TMessage; ComboWnd: HWnd;
ComboProc: Pointer);
var
rc, len: Integer;
begin
inherited;
case Message.Msg of
WM_KEYUP:
begin
// test to see if its a character that should not be processed.
if (Message.WParam <> 8) and (Message.WParam <> VK_DELETE) and
(Message.WParam <> VK_SHIFT) and (FTextCompletion = True) then
begin
// Use CB_FINDSTRING to locate the string in the Items property
rc := Perform(CB_FINDSTRING, -1, Integer(PChar(Caption)));
// if its in there then add the new string to the Text and
// select the portion that wasn't typed in by the user
if rc <> CB_ERR then
begin
// store the length of the current string
len := Length(Text); // set the new string
ItemIndex := rc; // highlight the rest of the text that was added.
SelStart := len;
SelLength := Length(Text) - len;
// return 0 to signify that the message has been handled.
Message.Result := 0;
end;
end;
end;
end; // case
end;end.
您的代码果真厉害,能在列表自动找到匹配的项
那么,同时如何动态过滤列表中的项呢,就是自动下拉并且只显示匹配的项。大侠,你看我是现在给分另起一个贴子等您回答,还是再次等候?