erdal Posted July 27, 2014 Posted July 27, 2014 how to use command uniedit---> Self.SelStart := Length(Self.Text); Self.SelLength := 0; And method change not found class list. procedure Change; override; problem - 1 Complete unit: //----------------------------------- Autocomplete edit-------------------------------------------------// unit AutocompleteEdit; interface uses Windows , Classes , Vcl.StdCtrls , SysUtils , StrUtils , Messages ,uniEdit ,uniGUIBaseClasses ,uniGUIClasses; const MSG_HIDEWORDLIST = WM_USER + 222; type TAutocompleteEdit=class(Tuniedit) private FWordList: TListBox; FCaretPos: Integer; FWordListHeight: Integer; FWordListWidth: Integer; procedure HandleWordListLostFocus(ASender: TObject); procedure HandleWordListSelectItem(ASender: TObject); procedure HandleWordListKeyPress(Sender: TObject; var Key: Char); procedure HandleWordListKeyDown(ASender: TObject; var Key: Word; Shift: TShiftState); procedure HandleHideWordList(var AMsg); overload; message MSG_HIDEWORDLIST; procedure HandleHideWordList; overload; procedure SetWordListHeight(const Value: Integer); procedure SetWordListWidth(const Value: Integer); procedure RegainFocus; protected procedure ShowWordList(AWords: TStrings); procedure HideWordList; procedure Change; override; procedure KeyDown(var Key: Word; Shift: TShiftState); override; procedure DoExit; override; public constructor Create(AOwner: TComponent); override; published property WordListHeight: Integer read FWordListHeight write SetWordListHeight; property WordListWidth: Integer read FWordListWidth write SetWordListWidth; end; TAutocompleteMan=class private FWords: TStrings; public constructor Create; destructor Destroy; override; function IsRecognized(AWord: string): Boolean; overload; function IsRecognized(AWord: string; AWordList: TStrings): Boolean; overload; procedure LoadFromFile(const AFilename: string); procedure AddWord(const AWord: string); property Words: TStrings read FWords; end; procedure Register; var AutocompleteMan: TAutocompleteMan; implementation procedure Register; begin RegisterComponents('CodeCall', [TAutocompleteEdit]); end; { TAutocompleteMan } procedure TAutocompleteMan.AddWord(const AWord: string); begin FWords.Add(UpperCase(AWord) + '=' + AWord); end; constructor TAutocompleteMan.Create; begin FWords := TStringList.Create; TStringList(FWords).Duplicates := dupIgnore; end; destructor TAutocompleteMan.Destroy; begin FWords.Free; inherited; end; function TAutocompleteMan.IsRecognized(AWord: string): Boolean; var i: Integer; begin Result := False; AWord := UpperCase(AWord); for i := 0 to FWords.Count-1 do begin Result := System.Pos(AWord, FWords.Names) > 0; if Result then Break; end; end; function TAutocompleteMan.IsRecognized(AWord: string; AWordList: TStrings): Boolean; var i: Integer; begin Result := False; AWord := UpperCase(AWord); AWordList.Clear; for i := 0 to FWords.Count-1 do begin if System.Pos(AWord, FWords.Names) > 0 then begin Result := True; AWordList.Add(FWords.ValueFromIndex); end; end; end; procedure TAutocompleteMan.LoadFromFile(const AFilename: string); var i: Integer; F: TStrings; begin F := TStringList.Create; try F.LoadFromFile(AFilename); for i := 0 to F.Count-1 do AddWord(F); finally F.Free; end; end; { TAutocompleteEdit } procedure TAutocompleteEdit.Change; var S: TStrings; begin inherited; if AutocompleteMan.IsRecognized(Self.Text) then begin S := TStringList.Create; try if AutocompleteMan.IsRecognized(Self.Text, S) then ShowWordList(S); finally S.Free; end; end else HideWordList; end; procedure TAutocompleteEdit.HandleHideWordList(var AMsg); begin HandleHideWordList; end; constructor TAutocompleteEdit.Create(AOwner: TComponent); begin inherited; FWordListHeight := 60; end; procedure TAutocompleteEdit.DoExit; begin if Assigned(FWordList) and FWordList.Visible and not FWordList.Focused then HideWordList; inherited; end; procedure TAutocompleteEdit.HandleHideWordList; begin FWordList.Free; FWordList := nil; end; procedure TAutocompleteEdit.HandleWordListKeyDown(ASender: TObject; var Key: Word; Shift: TShiftState); begin if (Key=VK_UP) and (FWordList.ItemIndex=0) then RegainFocus; end; procedure TAutocompleteEdit.HandleWordListKeyPress(Sender: TObject; var Key: Char); begin case Key of #13: begin Key := #0; Self.Text := FWordList.Items[FWordList.ItemIndex]; Self.SetFocus; Self.SelStart := Length(Self.Text); Self.SelLength := 0; HideWordList; end; #27: begin RegainFocus; HideWordList; end; else begin RegainFocus; end; end; end; procedure TAutocompleteEdit.HandleWordListLostFocus(ASender: TObject); begin if not Self.Focused then HideWordList; end; procedure TAutocompleteEdit.HandleWordListSelectItem(ASender: TObject); begin Self.Text := FWordList.Items[FWordList.ItemIndex]; HideWordList; end; procedure TAutocompleteEdit.HideWordList; begin PostMessage(Self.Handle, MSG_HIDEWORDLIST, 0, 0); end; procedure TAutocompleteEdit.KeyDown(var Key: Word; Shift: TShiftState); begin if Key=VK_ESCAPE then HideWordList else if (Key=VK_DOWN) and Assigned(FWordList) and FWordList.Visible then begin FCaretPos := Self.SelStart; FWordList.SetFocus; if FWordList.ItemIndex < 0 then FWordList.ItemIndex := 0; end else inherited; end; procedure TAutocompleteEdit.RegainFocus; begin Self.SetFocus; Self.SelStart := FCaretPos; Self.SelLength := 0; end; procedure TAutocompleteEdit.SetWordListHeight(const Value: Integer); begin if FWordListHeight <> Value then begin FWordListHeight := Value; if Assigned(FWordList) then FWordList.Height := FWordListHeight; end; end; procedure TAutocompleteEdit.SetWordListWidth(const Value: Integer); begin if FWordListWidth <> Value then begin FWordListWidth := Value; if Assigned(FWordList) then begin if FWordListWidth < 1 then FWordList.Width := Self.Width else FWordList.Width := FWordListWidth; end; end; end; procedure TAutocompleteEdit.ShowWordList(AWords: TStrings); begin if FWordList=nil then begin FWordList := TListBox.Create(Self); FWordList.ParentCtl3D := False; FWordList.Ctl3D := False; FWordList.Parent := Self.Parent; FWordList.TabStop := False; FWordList.OnExit := HandleWordListLostFocus; FWordList.OnKeyPress := HandleWordListKeyPress; FWordList.OnKeyDown := HandleWordListKeyDown; end; FWordList.Items.Assign(AWords); if FWordListWidth < 1 then FWordList.SetBounds(Self.Left, Self.Top + Self.Height, Self.Width, FWordListHeight) else FWordList.SetBounds(Self.Left, Self.Top + Self.Height, FWordListWidth, FWordListHeight); FWordList.Show; end; initialization AutocompleteMan := TAutocompleteMan.Create; finalization AutocompleteMan.Free; end. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.