Solved

Auto Complate in Tmemo or Tedit

Posted on 2006-07-07
7
545 Views
Last Modified: 2010-04-05
hi
how i can use or create AutoComplete in Tmemo or Tedit Such as tCombobox Autocomplete?
0
Comment
Question by:mahdiparak
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17057758
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17057837
procedure TForm1.Edit1Change(Sender: TObject);
var
  s : TStrings;
  i : integer;
  Tx : String;
begin
  if Edit1.Text = '' then Exit;
  Edit1.OnChange := nil; // this will prevent to call the onchange when we perform autocomplete
  s := Memo1.Lines; // Memo contains the list of keywords to search for
  Tx := Edit1.Text;
  // search the typed text
  for i := 0 to s.Count - 1 do
    if Copy(s[i], 1, Length(Tx)) = Tx then // look for partial match
    begin
      Edit1.Text := s[i];
      Edit1.SelStart := Length(Tx);
      Edit1.SelLength := Length(Edit1.Text) - Edit1.SelStart;
    end;
  Edit1.OnChange := Edit1Change; // reset the onchange handler
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  SelLength : Integer;
begin
  if (Key = VK_BACK) and (Edit1.SelStart > 0) then
  begin
    SelLength := Edit1.SelLength;
    Edit1.SelStart := Edit1.SelStart - 1;
    Edit1.SelLength := SelLength + 1;
  end;
end;
0
 
LVL 1

Author Comment

by:mahdiparak
ID: 17058330
thanks, but how i can do this in any application such az Notepad or any input dialog box in any windows application?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:atul_parmar
ID: 17058464
If you want it to work for other applications, then
1. you will need to hook the keyboard.
2. On each key press, find the window (e.g. Notepad, input box) what will receive the key
3. If it is text window, pick up/identify the word
4. search the word in your dictionary
5. copy and select your suggessted word

The keyboard hook is required to filter and notify your application when user presses a key. e.g. pressing backspace / delete will require special handling.
0
 
LVL 1

Author Comment

by:mahdiparak
ID: 17058543
dear atul_Parmar please show a code example for step 5
0
 
LVL 1

Author Comment

by:mahdiparak
ID: 17058576
please show code Worked in Notepad
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 500 total points
ID: 17059060
Hi, take a look at the following

var
  h : THandle;
  Buf : array [0..255] of char;
  Words, AutoWord : String;
  i : integer;
  FSL : TStringList;
begin
  FSL := TStringList.Create; // this will contain your dictionery
  h := GetForegroundWindow;
  GetWindowText(h, buf, 255);
  Words := 'S'; //
  if Buf = 'Untitled - Notepad' then
  begin
    for i := 0 to FSL.Count - 1 do
      if Copy(FSL[i], 1, Length(Words)) = Words then // look for partial match
        AutoWord := FSL[i];
    // send the key strokes
    for i := Length(Words) + 1 to Length(AutoWord) do
    begin
      keybd_event(VkKeyScan(AutoWord[i]), 1, 0, 0);
      keybd_event(VkKeyScan(AutoWord[i]), 1, KEYEVENTF_KEYUP, 0);
    end;
    // select the remaining character of autoword
    // press the shift key
    keybd_event(VK_SHIFT, 0, 0, 0);
    // now move left the cursor
    for i := Length(Words) + 1 to Length(AutoWord) do
    begin
      keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), 0, 0);
      keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), KEYEVENTF_KEYUP, 0);
    end;
    // release the shift key
    keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
  end;
end;
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question