Solved

Edit boxes 'n' case statements.

Posted on 1998-11-21
6
125 Views
Last Modified: 2010-04-04
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!!!
0
Comment
Question by:Mr_Plasmo
6 Comments
 
LVL 3

Expert Comment

by:philipleighs
ID: 1347492
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1347493
This doesn't work.  You would probably have to declare a new class and use the WM_KEYDOWN message.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1347494
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 10

Accepted Solution

by:
viktornet earned 50 total points
ID: 1347495
YOu can simple do

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

which is what meikl proposed,...
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1347496
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1347497
You deserve the points little man :o)
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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