Mega Code Archive

 
Categories / Delphi / Strings
 

How to correct badly inserted characters in edit to right numbers

Title: How to correct badly inserted characters in edit to right numbers ? Question: How to correct badly inserted numbers in edit to right numbers ? (if you set incorrectly keyboard language, for example Czech instead of English) - it also refuse to write any letters into edit (only numbers are alowed) - it also delete that annoing zero number in front of inserted number if you had cursor on the right side before insertion Answer: Add this procedure to your program: procedure ChangeOrFilterInputKeysInEdit(a: TObject; var k: Char); begin if k in ['¨¦'] then k := '0'; if k in ['+'] then k := '1'; if k in ['¨§'] then k := '2'; if k in ['?'] then k := '3'; if k in ['?'] then k := '4'; if k in ['?'] then k := '5'; if k in ['?'] then k := '6'; if k in ['y'] then k := '7'; if k in ['¨¢'] then k := '8'; if k in ['¨ª'] then k := '9'; if ((k in ['0'..'9']) or (k = char(08))) // "char(08)" is backspace then begin if (a as TEdit).Text = '0' then (a as TEdit).Text := ''; end else begin k := #0; end; end; How to use it: add OnKeyPress event for example to edit1 like this: (and insert ChangeOrFilterInputKeysInEdit procedure into it) procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin ChangeOrFilterInputKeysInEdit(Sender as TEdit, Key); end; ok that all ! note: this work only with Czech keyboard language, if you want it in another language, just set the wrong keyboard language (throught Alt - Shift) and press numbers 0 - 9 and write this characters into ['¨¦'] part of code, for example in Czech keyboard number 1 on keyboard mean character ¨¦ on screen. Tested in Delphi 2007, it works perfectly !