Recent

Author Topic: [SOLVED] how to disable the increment/decrement-arrows in TFloatSpinEdit  (Read 7420 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Hello,

TFloatSpinEdit shows 2 arrows for increment/decrement. I don't want them to see. Is there any possibility to hide them?
Or is there another control, which acts like TFloatSpinEdit for real values, but does not have these arrows?

Thanks a lot.
« Last Edit: October 01, 2016, 04:50:47 pm by Hartmut »

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #1 on: September 30, 2016, 05:27:19 pm »
Wow, TFloatSpinEdit without the spin arrows? I would use TEdit instead.

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #2 on: September 30, 2016, 05:48:42 pm »
Wow, TFloatSpinEdit without the spin arrows? I would use TEdit instead.

TFloatSpinEdit is ready for numbers: you can specify MaxValue and MinValue and the DecimalPlaces and nobody can enter letters and so on.
TEdit is not for numbers. If I specify 'NumbersOnly', then I cannot enter a decimal point or a minus sign.
TFloatSpinEdit would be perfect to me, if I could disable the 2 arrows. I tried 'Increment:=0' but this didn't work.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #3 on: September 30, 2016, 06:07:00 pm »
Okay, I understand.

You may try my solution. It's not the best, but it works.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   if (Key='.') or (Key=',') then Exit;
  4.   if (Key<'0') or (Key>'9') then Key := #0;
  5. end;

Put the 2 if conditions inside your TEdit.OnKeyPress event. I use 2 decimal point symbols because in some countries the decimal point is ','. You can change it depends your needs.

As I mentioned it's not the best, because you still have to validate the result (TEdit.Text) manually. There are several ways to make sure the result is a valid float value.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #4 on: September 30, 2016, 06:15:02 pm »
You may try to use a TFloatSpinEditEx (unit LazControls in Lazarus trunk) and then use a hack to access the protected property UpDown and set UpDown.Visible := False.

I have no idea if that works, nor wether it will behave nicely w.r.t. it's appearance on screen.

Bart


Hartmut

  • Hero Member
  • *****
  • Posts: 749
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #5 on: September 30, 2016, 06:43:58 pm »
Thanks to Handoko for your solution. It should work, but would be a lot of work for handling MaxValue and MinValue and the DecimalPlaces manually, because I need a couple of floating input fields. So I would prefer a solution which is nearer to TFloatSpinEdit, if there is any.

Thanks to Bart for your solution. Do I understand you right, that I need a very new version of unit "LazControls", which I must get from the trunk, so it would be not a tested and released version?

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #6 on: September 30, 2016, 07:18:38 pm »
Thanks to Handoko for your solution. It should work, but would be a lot of work for handling MaxValue and MinValue and the DecimalPlaces manually, because I need a couple of floating input fields. So I would prefer a solution which is nearer to TFloatSpinEdit, if there is any.

Yes, that's why I said that is not the best solution.

But actually it's not so hard. Just need to do these things:

1. Write a filter procedure once and use it on all the TEdit.OnKeyPress events. You know, that is the 2 if conditions code that you've just tried previously.

2. Pay attention on the decimal symbol, do you want to use a dot or a comma or depend on the user regional setting on the computer. If you want to get the the symbol based on the setting on the user's computer, then this info is useful:
http://www.freepascal.org/docs-html/rtl/sysutils/decimalseparator.html

3. Validate the TEdit.Text before you use it on your calculation. Pick one from these:
http://www.freepascal.org/docs-html/rtl/sysutils/strtofloat.html
http://www.freepascal.org/docs-html/rtl/system/val.html
Show an error message to user if the string can't be converted to a float variable.

4. If the string (TEdit.Text) can be converted to a float variable, check it again to make sure it is in the value range you want. Show an error message if the value is outside the range.

Note:
For step #3 and #4 can be combine into a single function, something like
function isValidValueInput(const S: string; var ResultVal: single; MinVal, MaxVal: single): Boolean;

It may seem difficult but once you've done it, you'll be glad you have learned something new. And you can save the code to reuse in the future. Challenge yourself to do something harder is one of the way to improve programming skill.
« Last Edit: September 30, 2016, 07:24:16 pm by Handoko »

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #7 on: September 30, 2016, 08:17:29 pm »
Thanks to Handoko for this very detailed answer. I could understand everything, although I am new to Lazarus. If there comes no more easy solution, I will try to implement this.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #8 on: September 30, 2016, 09:58:04 pm »
Thanks to Bart for your solution. Do I understand you right, that I need a very new version of unit "LazControls", which I must get from the trunk, so it would be not a tested and released version?

No, you need Lazarus trunk, not just LazControls package, because the component in question depends on LCL changes that are not available in the 1.6 branch.

Bart

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #9 on: October 01, 2016, 04:11:23 am »
Here are the codes, I haven't tested them but it should work.

Code: Pascal  [Select][+][-]
  1. procedure AllowOnlyFloatEntry(Sender: TObject; var Key: char);
  2. begin
  3.   if (Key='.') or (Key=',') or (Key=DecimalSeparator) or
  4.      (Key='-') or (Key='+') then Exit;
  5.   if (Key<'0') or (Key>'9') then Key := #0;
  6. end;

Copy/paste the code above into your source code. On the TEdit.KeyPress, click once and choose it from the list. Don't do double-click, because double-click means to write new code for the event.

Code: Pascal  [Select][+][-]
  1. function isValidFloatString(S: string; var ResultVal: Extended; MinVal, MaxVal: Extended): Boolean;
  2. var
  3.   C: Char;
  4.   i: Integer;
  5. begin
  6.  
  7.   Result := False;
  8.  
  9.   // Change non-standard decimal separator character to system decimal separator
  10.   for i := 1 to Length(S) do begin
  11.     C := S[i];
  12.     if (C=DecimalSeparator) or ((C>=0) and (C<=9)) then Continue;
  13.     S[i] := DecimalSeparator;
  14.   end;
  15.  
  16.   // Check if it is a valid float string
  17.   Try
  18.     ResultVal := StrToFloat(S);
  19.   except
  20.     On ResultVal : Exception do begin
  21.       ShowMessage('Please provide a valid value.');
  22.       Exit;
  23.       end;
  24.   end;
  25.  
  26.   // Check if the value is in the acceptable range
  27.   if (ResultVal>=MinVal) and (ResultVal<=MaxVal) then
  28.     Result := True
  29.   else
  30.     ShowMessage('Acceptable value range is '+FloatToStr(MinVal)+' to '+FloatToStr(MaxVal));
  31.  
  32. end;

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #10 on: October 01, 2016, 04:30:34 am »
You may try to use a TFloatSpinEditEx (unit LazControls in Lazarus trunk) and then use a hack to access the protected property UpDown and set UpDown.Visible := False.
Or use platform depended code right now. For example for windows:
Code: Pascal  [Select][+][-]
  1. ...
  2. uses Windows, Win32Proc;
  3.  
  4. {$R *.lfm}
  5.  
  6. { TForm1 }
  7.  
  8. procedure TForm1.FormActivate(Sender: TObject);
  9. begin
  10.   ShowWindow(GetWin32WindowInfo(FloatSpinEdit1.Handle)^.UpDown, SW_HIDE);
  11. end;

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #11 on: October 01, 2016, 09:08:29 am »
A rather cleaner one-liner for the OnKeyPress event handler looks like this:

Code: Pascal  [Select][+][-]
  1. if not ((Key in ['0'..'9'])
  2.       or (Key = #8)       //Backspace
  3.       or (Key = #127)     //Delete
  4.       or (Key = DefaultFormatSettings.DecimalSeparator)
  5.       or (Key = '-')
  6.       or (Key = '+'))
  7. then Key := #0;
  8.  
   

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #12 on: October 01, 2016, 12:16:41 pm »
I implemented the solution of ASerge, because it's only 1 line of code for each TFloatSpinEdit-Control. But I don't understand how that solution works. It think it disables the UpDown-Component of the TFloatSpinEdit-Control, but how? What does ShowWindow() and GetWin32WindowInfo() do here?
Thanks to everybody for your help.

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #13 on: October 01, 2016, 02:00:00 pm »
GetWin32WindowInfo: returns the handle of the UpDown buttons
ShowWindow coupled with SW_HIDE will hide the buttons.

keep in mind that this is platform specific solution and it will only work on windows.

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Re: how to disable the increment/decrement-arrows in TFloatSpinEdit
« Reply #14 on: October 01, 2016, 04:50:03 pm »
GetWin32WindowInfo: returns the handle of the UpDown buttons
ShowWindow coupled with SW_HIDE will hide the buttons.

keep in mind that this is platform specific solution and it will only work on windows.

ok, now I understand how it works. Thanks a lot.

 

TinyPortal © 2005-2018