Solved

onKeypress Event

Posted on 1998-09-28
7
413 Views
Last Modified: 2010-04-06
I would like to have a editbox in which we can only type numbers and the backspace. I think I have to use the onKeypress event and limit the access to ascii codes but the onkeypress event returns a char.  

Thanks
0
Comment
Question by:deha2228
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1340990
What about trapping the WM_CHAR message?

E.g.
In your component's declaration:
procedure WMChar(var Msg: TMessage); message WM_CHAR;

In your code:
procedure TMyEdit.WMChar(var Msg: TMessage);
begin
  // Only allow backspace, ENTER, Escape and 0..9
  if (TWMKey(Msg).CharCode in [8, 13, 27, 48..57]) then
    inherited;
end;

Cheers,
JB
0
 
LVL 2

Accepted Solution

by:
Thaddy earned 100 total points
ID: 1340991
The easy way would be to use a TMaskedEdit. It has all the features you want, Just create an editmask that satisfies your input limitations.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340992
Hello all!

I dont like TMaskEdit. It's kinda crappy. I rahter create my own EditBox as deha2228 is going to do. JimBob's solution is prefect for that task and is very easy to implement ;-)

Gotta get back to class ;->

Regards,
Viktor Ivanov
0
Industry Leaders: 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 27

Expert Comment

by:kretzschmar
ID: 1340993
hi deho2228,

help this codesample

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
const validchars = [#8,'0','1','2','3','4','5','6','7','8','9'];
begin
  if not(key in validchars) then key := #0
end;

meikl
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340994
Here is an easier way...

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not key in [#8, #13, #27 '.', '0'..'9'] then key := #0;
end;

Regards,
Viktor Ivanov
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1340995
hi viktor,

your comment is just the same, but i ask you for what do you use enter and Escapekey in an editfield?

well my const line could like shown as follows

      const validchars = [#8,'0'..'9'];

and the point if want to use can be added

meikl
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1340996
My point is that you could use the shorter way instead of writing all the chars like so...
const validchars = [#8,'0','1','2','3','4','5','6','7','8','9'];

I added some other characters indeed
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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