Delphi Pages Forums

Delphi Pages Forums (http://www.delphipages.com/forum/index.php)
-   General (http://www.delphipages.com/forum/forumdisplay.php?f=2)
-   -   HELP: My CHAR does not like to work (http://www.delphipages.com/forum/showthread.php?t=218661)

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.
Quote:

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

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

Code:

if not (key in [#48..#57,VK_BACK,VK_TAB,VK_CLEAR,VK_RETURN,VK_DELETE,VK_END,VK_HOME,VK_INSERT,VK_ESCAPE]) then
  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:
Code:

uses
  Clipbrd;


type

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

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

implementation

//add these methods after the implementation section

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

procedure TNumEdit.keypress(var Key: Char);
begin
if (key in [#48..#57,VK_BACK,VK_TAB,VK_CLEAR,VK_RETURN,VK_DELETE,VK_END,VK_HOME,VK_INSERT,VK_ESCAPE]) then
  inherited
else
  key = #0;
end;

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: https://www.thoughtco.com/virtual-ke...indows-4071289

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.



https://www.swissdelphicenter.ch/en/tipsindex.php //Norton AntiVirus warns about this site as a known dangerous site - they were probably hacked in the past

http://delphi.icm.edu.pl/

SvenLittkowski 03-17-2019 05:45 PM

Forbidden

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



-----


That is what http://delphi.icm.edu.pl/ 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.