⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qinccombobox.pas

📁 delphi 里做一个智能combo box 的程序。
💻 PAS
字号:
unit QIncCombobox;

interface

uses
  SysUtils, Classes, Qt, QControls, QStdCtrls;

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 = csDropDown) and not FComboDel then begin
//remember the current position
    OldPos := SelStart;
// search 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 = Key_Delete) then
    FComboDel := True;
end;

end.

⌨️ 快捷键说明

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