Filtering key presses in an edit control 
Author Message
 Filtering key presses in an edit control

Anyone have any suggestions on what may be the best way to add a filter to
an edit control?  I want to be able to keep a user for adding non alpha
numeric characters to an edit control.

I've attempted this before, but came up with a few messy solutions.  I am
going to try and tackle it again now...  my approach will be to override the
EN_CHANGE or EN_UPDATE and filter from within those messages, however I
already doubt this will work.  I don't think CHANGE is the message that
would work, due to the fact that the message is issued after the control has
been visually updated to the display.  UPDATE may be my best bet, due to the
fact that it is just before the edit control is to redraw itself...
However, I think I may have tried this in the past, by checking the last
character in the string and removing it based on validity, but it always
seemed to display the previous value of the string (i.e.: the next UPDATE
would have the character removed).

Another alternative I was thinking about is filtering the parent windows
WM_KEYDOWN message or some other I haven't looked into yet and see if I can
filter before going to the child control...

Thanks in advance for any advice.

--
Tobin S.
"Give me a compiler and I'll build you a universe."
http://www.*-*-*.com/ ~tobins



Wed, 10 Oct 2001 03:00:00 GMT  
 Filtering key presses in an edit control
Hi Tobin:

     Your best bet is to override PreTranslateMessage().  This is what I did.

Francisco J. Novas
P.S. Please remove .NOSPAM from return e-mail address

Quote:

> Anyone have any suggestions on what may be the best way to add a filter to
> an edit control?  I want to be able to keep a user for adding non alpha
> numeric characters to an edit control.

> I've attempted this before, but came up with a few messy solutions.  I am
> going to try and tackle it again now...  my approach will be to override the
> EN_CHANGE or EN_UPDATE and filter from within those messages, however I
> already doubt this will work.  I don't think CHANGE is the message that
> would work, due to the fact that the message is issued after the control has
> been visually updated to the display.  UPDATE may be my best bet, due to the
> fact that it is just before the edit control is to redraw itself...
> However, I think I may have tried this in the past, by checking the last
> character in the string and removing it based on validity, but it always
> seemed to display the previous value of the string (i.e.: the next UPDATE
> would have the character removed).

> Another alternative I was thinking about is filtering the parent windows
> WM_KEYDOWN message or some other I haven't looked into yet and see if I can
> filter before going to the child control...

> Thanks in advance for any advice.

> --
> Tobin S.
> "Give me a compiler and I'll build you a universe."
> http://www.mindspring.com/~tobins



Wed, 10 Oct 2001 03:00:00 GMT  
 Filtering key presses in an edit control
In addition, you must do some of the processing you mentioned, as the user
might - *gasp* - cut and paste into the control.
Something like:

 CString strNew;

UpdateData();
if(m_strEdit1.GetLength())
{
    for (int t = 0; t < m_strEdit1.GetLength();t++)
        if(m_strEdit1[t]!='o')
            strNew += m_strEdit1[t];

    m_strEdit1 = strNew;
    UpdateData(FALSE);
    m_ctrlEdit1.SetSel(m_strEdit1.GetLength(),m_strEdit1.GetLength());

Quote:
}

in the EN_CHANGE-handler might do the trick. The above assumes a value
mapping m_strEdit1 and control mapping m_ctrlEdit1 for the editbox, and will
refuse to let the user enter the letter 'o'. As you can validate, this
snippet refuses to paste 'o's as well.

Johan Rosengren
Responsable Informatique
PACTA S.A.

Francisco J. Novas a crit dans le message

Quote:
>     Your best bet is to override PreTranslateMessage().  This is what I

did.


Thu, 11 Oct 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Filter keys for edit control

2. Bell sound when ENTER is pressed in an EDIT control child of an ATL control

3. Getting the pressed Key in an edit field?

4. Help Edit Tree Item on Keyboard press or button press

5. Help Edit Tree Item on Keyboard press or button press

6. know when shift or control keys are pressed and unpressed

7. Checking whether control key is pressed

8. delete key press in list control

9. Edit Control...Edit Control...Edit Control...Edit Control...Edit Control...

10. Left Shift Key OR Right Shift key is pressed

11. On Key? , Key press

12. On Key? , Key press

 

 
Powered by phpBB® Forum Software