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
Solved

onKeypress Event

Posted on 1998-09-28
7
411 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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: how to send PJL commands to printer 3 108
add combobox item based on numbers 9 154
code issue 8 153
HTML text in the body of an email (delphi code) 12 166
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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