Edit boxes 'n' case statements.

I need a simple case statement for locking out everything but letters on an edit box's key-press. It's real easy but i've forgotten. PLEASE HELP ME!!!
Mr_PlasmoAsked:
Who is Participating?
 
viktornetConnect With a Mentor Commented:
YOu can simple do

OnKeyPress()
begin
  if not (key in ['a'..'z', 'A'..'Z']) then key := #0;
end;

which is what meikl proposed,...
0
 
philipleighsCommented:
I the KeyDown event, type:

case Key of
VK_A..VK_Z:
   begin
       //do nothing to allow key to be typed
   end;
else
    begin
          //Next line will prevent char allearing in edit box
         Key := 0;
    end;
end;


There you have it. If you don't like windows VK_ constants, you can replace it with Ord('A')..Ord('Z')

Cheers,
Phil.

0
 
scrapdogCommented:
This doesn't work.  You would probably have to declare a new class and use the WM_KEYDOWN message.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
kretzschmarCommented:
hi Mr_Plasmo,

that's no case-statement, but this is a simple solution to allow only letters,

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
const validchars : set of char = ['A'..'Z','a'..'z'];
begin
  if not(key in validchars) then key := #0;
end;

meikl
0
 
scrapdogCommented:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (key in ['a'..'z', 'A'..'Z', #8]) then key := #0;
end;


I would suggest including #8 so the user can backspace.
0
 
viktornetCommented:
You deserve the points little man :o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.