Jump to content

how to 'test' entered letter.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
2710

2710

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts
So I have an edit box, and I type some letters in. I want a live detector to test if the letters entered are letters or not. If it isn't a letter, I want it to display an error message.

I used editbox.text, but I realise that this tests the WHOLE box, rather than the currently entered character. I can't really test the 'last' letter, since I can input chars into the middle.

And I would rather not test the whole text everytime, letter by letter, since that is inefficient. SO I was just wondering if there is a function, or an easier way to do this?

Thanks

#2
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
Try the OnKeyPress event. This will beep if you press something not a letter


procedure TFormMain.EditKeyPress(Sender: TObject; var Key: Char);

begin

  if not(Key in ['a'..'z', 'A'..'Z']) then

  begin

    Beep;

    Key := #0;

  end;

end;



#3
2710

2710

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts

alienkinetics said:

Try the OnKeyPress event. This will beep if you press something not a letter

procedure TFormMain.EditKeyPress(Sender: TObject; var Key: Char);
begin
  if not(Key in ['a'..'z', 'A'..'Z']) then
  begin
    Beep;
    Key := #0;
  end;
end;

Thanks :D

#4
2710

2710

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts
Um...bit of a problem...

since i did a..z and A..Z, I can't delete what I have done with backspace. I guess its because Backspace is not part of a..z and A..Z? So how would I fix this?

The strange thing is that 'Delete' works, but not backspace...hmmm.

And that beep sound is awesome, are there anymore? :P

Thanks

#5
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
O, forgot. Sorry. Try this to include the special codes:

['a'..'z', 'A'..'Z', #1..#31]

If you ever need the code to a key press, add this code:

ShowMessage(IntToStr(Ord(Key)));

The Beep sound is the system default. ie: What the user selected. There is the function PlaySound() in the MMSystem unit which will play some more sounds.

Enumerating and Playing System Sounds from Delphi code