inccombobox.pas

来自「delphi 里做一个智能combo box 的程序。」· PAS 代码 · 共 81 行

PAS
81
字号
unit IncCombobox;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TIncCombobox = class(TCombobox)
  private
    { Private declarations }
    FComboDel : Boolean;
  protected
    { Protected declarations }
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure Change; override;
  public
    { Public declarations }
    procedure AddOrMoveItem(Item : String);
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TIncCombobox]);
end;

{ TIncCombobox }

procedure TIncCombobox.AddOrMoveItem(Item: String);
var
  CurrItem : Integer;
begin
  CurrItem := Items.IndexOf(Text);
  if CurrItem < 0 then
// if the item is not found on the list, add it at the first position
    Items.Insert(0,Text)
  else
// if the item already exists, it is moved to the first position
    Items.Move(CurrItem,0);
end;

procedure TIncCombobox.Change;
var
  NewItem, OldPos : Integer;
  TextLen : Integer;
begin
  inherited;
  if (Style in [csDropDown,csSimple]) and not FComboDel then begin
//remember the current position
    OldPos := SelStart;
// seach if item is already in the combobox
    TextLen := Length(Text);
    for NewItem := 0 to Pred(Items.Count) do
      if AnsiCompareText(Text,Copy(Items[NewItem],1,TextLen)) = 0 then
        break;
// if a match was found then set the text to the item found
    if NewItem < Items.Count then
      Text := Items[NewItem];
// set the selection from the original position to the end of the text
    SelStart := OldPos;
    SelLength := Length(Text);
  end;
  FComboDel := False;
end;

procedure TIncCombobox.KeyDown(var Key: Word; Shift: TShiftState);
begin
  inherited;
  if (Key = 8) or (Key = VK_DELETE) then
    FComboDel := True;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?