Solved

TEdit OnKeyPress Event help

Posted on 2010-09-13
4
361 Views
Last Modified: 2012-05-10
I have a function that converts a number to roman and visa versa. (thanks to EE expert).

I need to prevent users from entering anything other than the following characters

0..9, or  I,V,X,L,C,D,M

into a TEdit control

I guess i need to allow a backspace, but nothing else. (example, no spaces, periods, etc. etc)

thanks
0
Comment
Question by:Looking_4_Answers
  • 2
4 Comments
 

Author Comment

by:Looking_4_Answers
ID: 33666498
sorry, i meant to rephrase it

if not (Key in [#8, '0'..'9', 'I','V','X','L','C','D','M']) then
  Key := #0;

i need to prevent the person from entering a numeric if they are already entering a roman numeral character, and visa versa (if the enter a 'I','V','X','L','C','D','M'  then they can't enter a 0..9
0
 
LVL 25

Accepted Solution

by:
epasquier earned 250 total points
ID: 33666932
Voila !
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
Var
 Txt:String;
 i64:Int64;
 SetAllowed:Set of Char;
Const
 AllowDigits=['0'..'9'];
 AllowRomans=['I','V','X','L','C','D','M'];
begin
 Txt:=(Sender As TEdit).Text;
 if Txt='' Then SetAllowed:=AllowDigits+AllowRomans Else
  if TryStrToInt64(Txt,i64)
   Then SetAllowed:=AllowDigits
   Else SetAllowed:=AllowRomans;
 SetAllowed:=SetAllowed+[#8];
 Key:=UpCase(Key); // convert i, v, x... to I, V, X
 If Not (Key In SetAllowed) Then Key:=#0;
end;

Open in new window

0
 
LVL 9

Expert Comment

by:Mahdi78
ID: 33667012
Great !
Très bonne idée.
0
 

Author Closing Comment

by:Looking_4_Answers
ID: 33674904
NICE JOB!  Thanks!
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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…

840 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