entering -10 in a numbers only tedit lazarus / 31-LCL ---------------------------------------------------------------------- speter · 2021-02-02 06:53 G'Day Folks, This is probably "operating as intended"; but if I have a Tedit with .numbersonly=true; when I run the program, I get a popup error if I try to enter "-10" (meaning I can't enter the "-"). Is "numbersonly" interpreted as positive numbers only (literally chars 0..9)? If I paste "-10" into the edit, the program does not complain. Similarly, if I try to type 1.5 (I can't enter the decimal point); but I can't paste "1.5"! cheers S. ---------------------------------------------------------------------- Bart · 2021-02-02 08:37 According to MS, -10 is NOT a number  NumbersOnly apparently meant to the MS developers that every character you enter/paste in the control must be a number. On Windows, NumersOnly is implemented by giving the OS control over this. On other platforms it is either not implemented, or implemented in a similar fashion to the MS behaviour. In hindsight I regret that I did implement it (it was a user request)  There are better alternatives for numeric input: TSpinEdit/TSpinEditEx (integers), TFloatSpinEdit(Ex) (floating point values), or TMaskEdit, which gives you control over everything the user can enter at any given position (but it looks a bit more ugly). If you dislike the spinners for the various SpinEdit controls, IIR you can hide them. Bart ---------------------------------------------------------------------- speter · 2021-02-02 09:46 Thanks Bart. ---------------------------------------------------------------------- ArtLogi · 2021-02-02 16:01 Use regular edit-field and introduce events. This is one option, then maybe also add routine to check if the entered string is actually formatted like it should be. A few years ago I had severe problems with the TMaskEdit, to get it to work even close to satisfactory.  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);begin(* Consider changing '.' to DecimalSeparator as srtToFloat conversion is now localised in freepascal and needs to have dot or comma depending your OS settings. The variable DecimalSeparator contains the right decimal separator for FPC and which is used at your OS localisation *)  if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;  // Now the edit field do not accept any other characters from keyboard input than those listed inside bracketsend; ---------------------------------------------------------------------- Bart · 2021-02-02 19:17 Quote from: ArtLogi on February 02, 2021, 05:01:34 pmUse regular edit- field and introduce events. This is one option, then maybe also add routine to check if the entered string is actually formatted like it should be. And then people will start pasting non numeric things into the control. It's a PITA to handle that (see the mess in TMaskEdit. I know, I wrote it). IMO a TSpinEdit(Ex) would be the easiest solution. It has the advantage that it has a Value property: no need to do conversions (text to number) yourself. Quote from: ArtLogi on February 02, 2021, 05:01:34 pmA few years ago I had severe problems with the TMaskEdit, to get it to work even close to satisfactory.  Just curious: what did you want to achieve, and why was the end result not satisfactory? (For starters of course it's not esthetically pleasing.) Bart ---------------------------------------------------------------------- ArtLogi · 2021-02-02 22:17 Quote from: Bart on February 02, 2021, 08:17:57 pmQuote from: ArtLogi on February 02, 2021, 05:01:34 pmUse regular edit-field and introduce events. This is one option, then maybe also add routine to check if the entered string is actually formatted like it should be. And then people will start pasting non numeric things into the control. It's a PITA to handle that (see the mess in TMaskEdit. I know, I wrote it). IMO a TSpinEdit(Ex) would be the easiest solution. It has the advantage that it has a Value property: no need to do conversions (text to number) yourself. Quote from: ArtLogi on February 02, 2021, 05:01:34 pmA few years ago I had severe problems with the TMaskEdit, to get it to work even close to satisfactory.  Just curious: what did you want to achieve, and why was the end result not satisfactory? (For starters of course it's not esthetically pleasing.) Bart I'm not sure but something related to numbers, then I did pick that approach quoted earlier (found from old local "Self-learn Delphi programming, covers delphi 1&2" book), which is older than some of the forum member I would assume.  Unfortunately the 1.44" floppy have gone missing, but considering that I saved it from the book bon-fire, it is still good find. The problem IIRC is that the actual masking is not documented if I do click from Help-button in "mask editor" nothing happens. There is no examples no nothing (even now with L2.0.4 F3.0.4). Maybe there is somewhere.  Another thing like you said might have been that it is indeed a bit "ugly" if one is after for a clean look. I think it is a bit too specialized edit field for most, but no doubt priceless in many more technical fields than just to enter number (when the masking rules are clear). Third thing is that IIRC I did use heavily this TlabeledEdit because of the label obviously. Or maybe it was not flexible after masked ... can't remember, sorry.  ....AND YES I was already pissed off from the exact same finding with TEdit as OP above.  Here is upgraded version for edit-field... Unfortunately I do not know if this violates something and causes some severe issues (mainly in memory management), since it kind of have some recursion  ( Objects, you know ). Quickly tested, just proof of concept. Handles also copy paste from both keyboard and mouse. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // This handles the number input while editing the fieldprocedure TForm1.Edit2Change(Sender: TObject);var  TextValue: Double;begin  Case Edit2.text of     '-', '+', '' : ;  else    Try     TextValue := strToFloat( Edit2.text);    Except     on E:Exception do     begin     edit2.text := '0';       ShowMessage('Numbers only!');     end;    end;  end;end;  // This handles the -+(empty) while field is not edited anymore and focus is changed to somewhere else.procedure TForm1.Edit2Exit(Sender: TObject);var  TextValue: Double;begin    Try     TextValue := strToFloat( Edit2.text);    Except     on E:Exception do     begin       edit2.text := '0';       ShowMessage('Numbers only!');     end;    end;end; Oops. The first with only Change-event didn't like the minus nor plus nor empty field.  This seems to be ok on that regard. ---------------------------------------------------------------------- Bart · 2021-02-03 11:55 That's ab it ugly IMO. If I type 123A, it will reset it to 0. W.r.t. TMaskEdit: in the sources, you can find what all the tokens in the editmaks mean. Also there is the TMaskEdit wiki page. I used TMaskEdit al lot in some of my old Delphi3 programs. When I moved to Lazarus, TMaskEdit was rather broken, so I contributed many bugfixes for that. Once I had done that, I had already discovered T(Float)SpinEdit (D3 does not have that), which suited my needs better than TMaskEdit, so I ended up completely rewriting the application. Nowadays I would not TMaskEdit it for numerical input. It is however a nice control to enter zip-codes, IP adresses etc. Bart Bart