Reputation: 13
Ran into a couple of problems using a code that only lets the user input alphabetical characters and a backspace.
When I compile my program using RAD studio 2010, apart from Vcl problems in the Uses clause, It compiles correctly, all working fine. However when I try and compile using XE5, I get an error: E2010 Incompatible types: 'Word' and 'AnsiChar'
If anyone can point me in the right direction it would be great!
Code below:
procedure TFRMStuTakeTest.DBEDTWord01KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
(*only return value if alphabetic *)
if Key in ['A'..'Z', 'a'..'z', #8] then
else
Key := #0;
end;
Sorry if there's something about procedures having to be from clean projects (i.e. unnamed/ not descriptive)
Upvotes: 1
Views: 2854
Reputation: 125671
OnKeyDown
is designed to deal with virtual key codes (those that are referred to using the VK_
constants), not individual letter and number keystrokes.
Use OnKeyPress
to process individual characters, not OnKeyDown
.
procedure TFRMStuTakeTest.DBEDTWord01KeyPress(Sender: TObject; var Key: Char);
begin
if not CharInSet(Key, ['A'..'Z','a'..'z', #8]) then
Key := #0;
end;
Better still, use the EditMask
on the underlying TField
, and set a valid mask for alpha characters using something like 'LLLLL;0;_'
, which would require 5 letters between '['A'..'Z','a'..'z']', and will handle all of the validations, editing keystrokes, and so forth.
YourTable.FieldByName('Word1').EditMask := 'LLLLL;0;_';
See the documentation for TField.EditMask for more information, and follow the link at the bottom to TEditMask for details about the mask characters. (There's an editor for the EditMask
property in the Object Inspector; they're the same as those used by TMaskEdit
, so you can drop one of those on your form and click the ...
button on the right side of the EditMask
property to access it.)
Upvotes: 4