Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Autocomplete TEdit

tedit autocomplete

  • Please log in to reply
No replies to this topic

#1 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 07 July 2013 - 05:58 AM

[Note: this article is still a work in progress. Please visit again in near future to see the final version.]

 

Overview

 

Autocomplete feature really helpful for us speeding up our typing job. For you who is not familiar with the term autocomplete, it's when you type partial part of a word and then you will be presented with a list of possible complete words. You can just select the correct word from the list, and that partial word will be automatically completed. In programming, this feature very helpful to "remember" class names, routine names, and variable name.

 

Not only to speed up the typing, autocomplete also very helpful to avoid typo.

 

In this tutorial I will show you a technique to implement autocomplete in your Delphi program in order to provide your users the benefits of autocomplete. I will implement autocomplete in a descendant of TEdit. I name it TAutocompleteEdit.

 

 

TAutoCompleteEdit

 

Behaviors

  1. Upon typing some chars, TAutocompleteEdit will check the typed word agains a word list. When no match found, do nothing.
  2. When one or more matches found, TAutocompleteEdit show the matches in a TListBox.We will call this TListBox WordList.
  3. User can move between TAutocompleteEdit and WordList using down and up arrow.
  4. User select a complete word from WordList by highlighting the word and press Enter key.
  5. After user selected a word, the word will replace whatever content in TAutocompleteEdit.
  6. If user press Escape in TAutocompleteEdit or in WordList, WordList must dissapear.
  7. If TAutocompleteEdit lost focus, and the new focus is not in WordListWordList must dissapear.
  8. If WordList lost focus, and the new focus is not in TAutocompleteEditWordList must dissapear.
  9. If later user type in another character and no match found, WordList must dissapear.

 

Key Methods

 

From the above behaviors, we decided to have the following methods.

  1. ShowWordList(AWords: TStrings).
    This method is responsible to create WordList TListBox when needed, populate it with words contained in AWords, and also to patch its events so we can achieve behavior #3, #4, #5, #6, and #8.
  2. HideWordList.
    This method is responsible to hide and clean up WordList.
  3. Change.
    This is where to respond when the content of TAutocompleteEdit changed. So this is where we do the checking. This method actually already exist in TAutocompleteEdit's parent. So what we are going to do is override it, and introduce our behavior.
  4. DoExit.
    This method also already exist in TAutocompleteEdit's parent. We are going to override it and introduce new behavior, in order to achieve behavior #7.
  5. KeyDown(var Key: Word; Shift: TShiftState).
    This method also already exist in TAutocompleteEdit's parent. We are going to override it to achieve behavior #3 and #6.

 

Key Methods Implementations

 

1. ShowWordList(AWords: TStrings).



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;

2. HideWordList



procedure TAutocompleteEdit.HideWordList;
begin
  PostMessage(Self.Handle, MSG_HIDEWORDLIST, 0, 0);
end;

Note that in the above method we only post a custom message. The custom message handler in turn will call this private method.



procedure TAutocompleteEdit.HandleHideWordList;
begin
  FWordList.Free;
  FWordList := nil;
end;

3. Change.



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;

4. DoExit.



procedure TAutocompleteEdit.DoExit;
begin
  if Assigned(FWordList) and FWordList.Visible and not FWordList.Focused then
    HideWordList;
  inherited;
end;

5. KeyDown(var Key: Word; Shift: TShiftState).



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;

Here is the complete source code of TAutocompleteEdit: Attached File  TEdit with Autocomplete.zip   1.84MB   2470 downloads. Feel free to use it or improve it for any kind of use.

 

Cheers!


  • 0





Also tagged with one or more of these keywords: tedit, autocomplete

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download