Solved

onKeypress Event

Posted on 1998-09-28
7
407 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now