Solved

onKeypress Event

Posted on 1998-09-28
7
412 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
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
Independent Software Vendors: 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi application Soap connection 5 131
Dynamically Created Query 3 69
How to Get Images From Server using App Tethering 11 42
Firemonkey Queue list 1 25
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…

713 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