声明两个全局变量
var aStringList: TStringList; //读取 关键字 aMemoInput:string; //当前 已输入 项
procedure TSearchReplaceDemoForm.FormCreate(Sender: TObject); begin aStringList:=TStringList.Create; aStringList.LoadFromFile('keyWord.txt'); //从文件加载 关键字 end;
拖一个ListBox,命名为ListBox_keyWords
procedure TSearchReplaceDemoForm.ListBox_keyWordsKeyPress(Sender: TObject; var Key: Char); var tempStr:string; begin if key=#27 then //esc begin ListBox_keyWords.Visible:=false; SynEditor.SetFocus; aMemoInput:=''; end; if key =#13 then begin //回车键 tempStr:= ListBox_keyWords.Items[ListBox_keyWords.ItemIndex] ; SynEditor.SelText := rightstr( tempStr ,length(tempStr)-length(aMemoInput)); ListBox_keyWords.Visible:=false; SynEditor.SetFocus; aMemoInput:=''; end; end;
鼠标双击,选择
procedure TSearchReplaceDemoForm.ListBox_keyWordsDblClick(Sender: TObject); begin var tempStr: string; begin if ListBox_keyWords.ItemIndex >= 0 then begin tempStr:= ListBox_keyWords.Items[ListBox_keyWords.ItemIndex] ; SynEditor.SelText := rightstr( tempStr ,length(tempStr)-length(aMemoInput)); ListBox_keyWords.Visible:=false; SynEditor.SetFocus; aMemoInput:=''; end; end; end;
假设SynEditor为Memo类型
procedure TSearchReplaceDemoForm.SynEditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if ( key=VK_DOWN) and (ListBox_keyWords.Visible) then begin //按 向下键,选择 ListBox_keyWords.SetFocus; ListBox_keyWords.ItemIndex:=0; exit; end; if ( key=VK_BACK) then begin // 向后删除键 aMemoInput:=leftstr(aMemoInput,length(aMemoInput)-1) ; ListBox_keyWords_Refresh(aMemoInput); exit; end; if ( key=VK_ESCAPE ) or(key= VK_RETURN) then begin // ESC 和 回车 ListBox_keyWords.Visible:= false; aMemoInput:=''; exit; end; end; procedure TSearchReplaceDemoForm.SynEditorKeyPress(Sender: TObject; var Key: Char); begin if CharInSet( key , ['a'..'z','A'..'Z','_']){ or (Key > #127)} then begin aMemoInput:=aMemoInput+key; ListBox_keyWords_Refresh(aMemoInput); end; if tulaterIsSeparator(key ) then begin //输入 间隔符, ListBox 消失 aMemoInput:='' ; ListBox_keyWords.Visible:=false; end; end;
标签:Delphi10.3,begin,end,AutoComplete,关键字,key,keyWords,aMemoInput,ListBox From: https://www.cnblogs.com/tulater/p/18431319