Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-25-2019, 04:24 AM
SvenLittkowski SvenLittkowski is offline
Member
 
Join Date: Jul 2010
Posts: 39
Default 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;
Reply With Quote
  #2  
Old 02-25-2019, 08:14 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

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)
Reply With Quote
  #3  
Old 02-25-2019, 09:35 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 198
Default

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...
Reply With Quote
  #4  
Old 03-14-2019, 06:56 PM
SvenLittkowski SvenLittkowski is offline
Member
 
Join Date: Jul 2010
Posts: 39
Default

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?

Last edited by SvenLittkowski; 03-14-2019 at 06:58 PM.
Reply With Quote
  #5  
Old 03-15-2019, 01:01 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 198
Default

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

Last edited by rojam; 03-15-2019 at 03:43 PM.
Reply With Quote
  #6  
Old 03-16-2019, 02:26 AM
SvenLittkowski SvenLittkowski is offline
Member
 
Join Date: Jul 2010
Posts: 39
Default

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?
Reply With Quote
  #7  
Old 03-17-2019, 03:35 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 198
Default

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/
Reply With Quote
  #8  
Old 03-17-2019, 05:45 PM
SvenLittkowski SvenLittkowski is offline
Member
 
Join Date: Jul 2010
Posts: 39
Default

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...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 07:21 PM.


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