Board index » delphi » TEdit with filter for certain keys

TEdit with filter for certain keys

Quote
Joachim Marder (mar...@uni-trier.de) wrote:

:  Hi Folks!

: I am trying to create a TEdit-like component, which accepts only a certain
: set of chars, like the TFilterValidator in TurboVision.
: I get the KeyUp/KeyDown/KeyPress-Messages, but the Edit-control gets and
: displays the character anyway.
: What must I do to get the key-messages before the Edit-control gets them.
: Or is there anyone who has a better idea to implement this?

:     Greetings from Germany
:                                        Joey
You could do it several ways:
OnKeyPress Event handles it like this:

begin
  if not (Key in ['0'..'9',#8]) then Key:=Chr(0);
end;
  (This one takes only digits and backspace, #8)

OnKeyDown handles it similar:
begin
  if not Key in (65..90,8,97..122,VK_DELETE) then Key:=0;
end;
  (This one takes only letters and backspace and Deletebutton)

It's real easy.

Good Luck

d4fi...@dtek.chalmers.se
Chalmers university of Technology

 

Re:TEdit with filter for certain keys


In <3v7ub1$...@news01.uni-trier.de>, mar...@uni-trier.de (Joachim Marder) writes:

Quote
>What must I do to get the key-messages before the Edit-control gets them.

        In the Edit1.Keypress(...) method, do something like:

if Key in ['0'..'9'] then
  Key := #0;

        The assignment of #0 to Key tells Delphi not to process it further.

Steve

Re:TEdit with filter for certain keys


 Hi Folks!

I am trying to create a TEdit-like component, which accepts only a certain
set of chars, like the TFilterValidator in TurboVision.
I get the KeyUp/KeyDown/KeyPress-Messages, but the Edit-control gets and
displays the character anyway.
What must I do to get the key-messages before the Edit-control gets them.
Or is there anyone who has a better idea to implement this?

    Greetings from Germany
                                       Joey

Re:TEdit with filter for certain keys


Quote
Filip Hanik (d4fi...@dtek.chalmers.se) wrote:

: Joachim Marder (mar...@uni-trier.de) wrote:
: :  Hi Folks!

: : I am trying to create a TEdit-like component, which accepts only a certain
: : set of chars, like the TFilterValidator in TurboVision.
: : I get the KeyUp/KeyDown/KeyPress-Messages, but the Edit-control gets and
: : displays the character anyway.
: : What must I do to get the key-messages before the Edit-control gets them.
: : Or is there anyone who has a better idea to implement this?

: :     Greetings from Germany
: :                                        Joey
: You could do it several ways:
: OnKeyPress Event handles it like this:

: begin
:   if not (Key in ['0'..'9',#8]) then Key:=Chr(0);
: end;
:   (This one takes only digits and backspace, #8)

: OnKeyDown handles it similar:
: begin
:   if not Key in (65..90,8,97..122,VK_DELETE) then Key:=0;
: end;
:   (This one takes only letters and backspace and Deletebutton)

: It's real easy.

That's right, but how can I trap special keys, as for example
<Ctrl>-<Cursor left>?
The key parameter in OnKeyDown is a var parameter, the shift parameter isn't,
so the following code doesn't work:

if [ssctrl] in Shift then Shift:=Shift-[ssctrl];

Any suggestions?

: Good Luck

: d4fi...@dtek.chalmers.se
: Chalmers university of Technology

Regards
Matthias
---
Matthias Romppel                   email: mrom...@gwdg.de
Institut fuer Psychologie
Gosslerstr. 14
D-37073 Goettingen

Re:TEdit with filter for certain keys


| That's right, but how can I trap special keys, as for example
| <Ctrl>-<Cursor left>?
| The key parameter in OnKeyDown is a var parameter, the shift parameter isn't,
| so the following code doesn't work:
| if [ssctrl] in Shift then Shift:=Shift-[ssctrl];
| Any suggestions?

Key:=0;
SendMessage(Edit1.Handle,WM_CHAR,<key code>, 0);

<key code> is the virtual key code.
--
Joseph S. Bui's Standard Disclaimer:
I love you.

Other Threads