delphi中Memo1中ListBox_keyWords代码补全

拖一个Memo1下拉ListBox到界面上,假设ListBox_keyWords,里已加载关键字

最终效果:

Memo1里输入关键字开头字母,自动带出ListBox_keyWords开头的关键字,选择好 关键字,回车,将关键字全称输入到Memo1原位置

全局变量aMemoInput代表,已输入的关键字开头字母

ListBox_keyWords_Refresh(aMemoInput);

更新ListBox_keyWords列表

procedure TForm8.ListBox_keyWords_Refresh(aInput:string);
var i:integer;a:string;
begin
if ainput='' then  begin  ListBox_keyWords.Visible:=false; exit; end;

 ListBox_keyWords.Items.Clear;
      for i := 0 to aStringList.Count -1 do begin
        if    sameText( leftStr( aStringList[i],length(aMemoInput))  ,aInput) then   begin
        ListBox_keyWords.Items.Add(aStringList[i])
      end;

      if ListBox_keyWords.Items.Count>0 then      begin
        a:= leftStr( Memo1.Lines[Memo1.CaretPos.Y ] , Memo1.CaretPos.x) ;
         ListBox_keyWords.Left:=length(a) * memo1.Font.Size+9;
         ListBox_keyWords.Top:=(memo1.CaretPos.Y-Memo1.Perform(EM_GETFIRSTVISIBLELINE,0,0)+2)*abs(memo1.Font.Height)+22;                  ;
         ListBox_keyWords.BringTofront;
         ListBox_keyWords.Visible:=True;
      end
      else  ListBox_keyWords.Visible:=false;
     end;
end;

procedure TForm8.ListBox_keyWordsKeyPress(Sender: TObject; var Key: Char);
var  tempStr:string;
begin
 if key=#27  then   //esc
     begin
     ListBox_keyWords.Visible:=false;
     memo1.SetFocus;
     aMemoInput:='';
     end;

  if key =#13  then  begin    //回车键

      tempStr:=  ListBox_keyWords.Items[ListBox_keyWords.ItemIndex] ;
      Memo1.SelText := rightstr(  tempStr ,length(tempStr)-length(aMemoInput));
      ListBox_keyWords.Visible:=false;
      memo1.SetFocus;
      aMemoInput:='';
  end;
end;

procedure TForm8.Memo1KeyPress(Sender: TObject; var Key: Char); 
begin
 if key in ['a'..'z','A'..'Z','_']  then   begin
   aMemoInput:=aMemoInput+key;
   ListBox_keyWords_Refresh(aMemoInput);
 end;
end;

procedure TForm8.Memo1KeyDown(Sender: TObject; var Key: Word;   Shift: TShiftState);
begin

if ( key=VK_DOWN)  and (ListBox_keyWords.Visible)     then begin //按 向下键,选择
  if ListBox_keyWords.CanFocus then ListBox_keyWords.SetFocus;
   ListBox_keyWords.ItemIndex:=0;
   key:=0;
   exit;
  end;

if ( key=VK_BACK)     then begin // 删除键
    aMemoInput:=leftstr(aMemoInput,length(aMemoInput)-1) ;
    ListBox_keyWords_Refresh(aMemoInput);
   key:=0;
   exit;
  end;

end;

 \

procedure TForm8.Memo1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 ListBox_keyWords.Visible:= False;
 aMemoInput:='';
end;