Delphi Pages Forums

Delphi Pages Forums (
-   General (
-   -   HELP: My CHAR does not like to work (

SvenLittkowski 02-25-2019 04:24 AM

HELP: My CHAR does not like to work
Hi, all I want is to ensure that no other input can be entered into an EDIT field than numeric values. I added an OnKeyPress procedure, where I check for the ORD value of the pressed keyboard button.

My thought was, if the input is not numeric, to replace the value of the pressed keyboard button witha nothing. But it seems, theere is some thinking error, and I get the following message:
Incompatible types: char and string

And here is the source code that gives me that famous error.

procedure TAlpha.EditBalloonVolumeKeyPress(Sender: TObject; var Key: Char);
if not(ord(Key)in[48..57]) then Key:='';

Norrit 02-25-2019 08:14 AM

Your quote quote ('') is not a character, so the compiler error is obvious
You could try Key := #0, but this will just return a null character. There's no real equivalent of nothing for a Char (therefor null would be the closest I guess)

rojam 02-25-2019 09:35 PM

You may also want to allow the backspace key and the Del Key in case the user mistypes the number he wants.

But there should be a better way to do this, can't confirm that all versions of Delphi has the property and I don't know which version you are using, regardless, look to see if the the Edit control has a "NumericOnly" property. You can set it in the object inspector to True and only numeric values will be allowed...

SvenLittkowski 03-14-2019 06:56 PM

Thanks, the #0 helped.

But indeed, the Backspace button is now disabled, you are right. No NUMERIC ONLY feature, I use Delphi 7. How to prevent the BACKSPACE and DEL buttons to become useless?

rojam 03-15-2019 01:01 PM


  key = #0;

You'll most likely still want most other special keys to work within your edit control.
i.e. VK_HOME takes cursor to first character, VK_END takes cursor past last cursor etc...
You'll need to see if the "numeric keyboard" keys works with the above code, if not, you'll need to add VK_NUMPAD0..VK_NUMPAD9

BTW: This is by no means a complete solution... The user will still be able to paste non-numeric values into the edit control

if you want to tackle that, you can try modifying your form as follows:



//add this declaration to the type section before your form's declaration
 TNumEdit = class(TEdit)
  procedure WMPaste(Var msg: TWMSettext ); message WM_PASTE;
  procedure keypress(var Key: Char); override;

  TForm1 = class(TForm)
    Edit1: TNumEdit; //change only the numeric TEdit control to be of type TNumEdit


//add these methods after the implementation section

procedure TNumEdit.WMPaste(var msg: TMessage);
  s: String;
  i: Integer;
  Valid: Boolean;
  Valid := True;
  S := Clipboard.AsText;
  for i := 1 to length(S) do
    if not (S[i] In ['0'..'9']) then
      Valid := False;
  if Valid then

procedure TNumEdit.keypress(var Key: Char);
  key = #0;

Remove the TForm1.Edit1KeyPress method, since the control itself is now handling that.

I don't have Delphi with me here, so I can't test this code, so there may be some changes required????

also, here is the list of VK_XX constants:

SvenLittkowski 03-16-2019 02:26 AM

Interesting, i will try that! Thanks.

Another question: there were, once, those famous "Delphi Super Page"s, one in Poland and one copy of that in the USA. But both stopped to function (links not doing their job anymore there). Is there any other website that has that amount of items for Delphi, maybe even the same ones?

rojam 03-17-2019 03:35 PM

Some of those old web sites are still around. //Norton AntiVirus warns about this site as a known dangerous site - they were probably hacked in the past

SvenLittkowski 03-17-2019 05:45 PM


You don't have permission to access /ftp/d30free/ on this server.


That is what shows me each time I want to download somethign. This site has the same problems like those Delphi Super Page(s) I mentioned. And that is so sad, as those pages are like treasure chests for Delphi developers.

I am trying out now that Swiss site...

All times are GMT. The time now is 07:04 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.