Adding proposed text to a CEdit as the User Types

118 Views Asked by At

In my MFC application, I am trying to offer help to users by proposing text as the user types.

The text the user is typing is left as typed, and the proposed text at the end is selected so the user can just hit delete if it is not what they want, or they can keep typing and my proposed test gets closer and closer to what they want.

My code (in a change handler for the CEdit control) looks like this :-

[Code to retrieve the current text]

[Code to get proposed text from my database back end]

(The user's text and the trailing extra proposed text are now in m_Eqpt)

m_EqptCtrl.SetWindowTextW(m_Eqpt);
m_EqptCtrl.SetSel(nTextLength, m_Eqpt.GetLength());

(nTextLength is the length of the text the user entered)

This works great as the user is typing, but if the user back-spaces or hilights and deletes text, my code is repeatedly adding the proposed text back in, where it should leave it alone at this point.

I'm at a loss to see how I can determine if the user has deleted my proposed text. Anyone solved a similar problem?

2

There are 2 best solutions below

1
On

Is the Shell autocomplete a way to solve your problem?

  1. Here the MSDN description for the Shell code

  2. Here another code sample from CodeProject, that should use a similar Approach than you do.

1
On

You should be able to detect the delete key on the edit field by trapping WM_KEYDOWN and checking that the edit field has the focus. Setting a flag to indicate the user has deleted your proposed text should help.