Recent

Author Topic: TUpDown increment type  (Read 3214 times)

Zath

  • Sr. Member
  • ****
  • Posts: 391
TUpDown increment type
« on: May 01, 2019, 02:09:27 am »
TUpDown

Hello.
Is there a way to allow this component to use Real or something other than Int ?
I'm using it to change money values and the increment insists on Ints but I want to work to two decimal places.

Thanks.


Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: TUpDown increment type
« Reply #1 on: May 01, 2019, 02:25:18 am »
Further to this, I see the TFloatSpinEdit allows what I want but the UpDown arrows are fixed and can't be manipulated like the TUpDown.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: TUpDown increment type
« Reply #2 on: May 01, 2019, 02:28:40 am »
Why can't you just divide by 100?

What do you mean by "fixed" and "manipulated"? What do you want the TFloatSpinEdit to do?
« Last Edit: May 01, 2019, 04:18:41 am by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: TUpDown increment type
« Reply #3 on: May 01, 2019, 09:56:11 am »
Why can't you just divide by 100?

What do you mean by "fixed" and "manipulated"? What do you want the TFloatSpinEdit to do?

TUpDown allows you to stretch the arrows, orientate them around a TEdit etc. The FloatEdit allows none of this. It might allow changing arrows to the left of its own edit but apart from that, you are unable to change it.

TUpDown has a facility to asscociate with an object, like a binding. When you use this, it restricts you to integers for the increment. You can add your value to the increment but the resultant value must be of type integer, TFloatSpinEdit by virtue, doesn't.


wp

  • Hero Member
  • *****
  • Posts: 11912
Re: TUpDown increment type
« Reply #4 on: May 01, 2019, 10:51:03 am »
Do not link the UpDown to the Edit (i.e., the Associate of the TUpDown must remain empty) to avoid the UpDown changing the Edit on its own. Then write this event handler for the OnClick event of the UpDown:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
  2. var
  3.   value: Double;
  4. begin
  5.   if not TryStrToFloat(Edit1.Text, value) then
  6.     MessageDlg('No number', mtError, [mbOK], 0)
  7.   else
  8.   if Button = btNext then
  9.     Edit1.Text := FormatFloat('0.00', value + UpDown1.Increment*0.01)
  10.   else
  11.     Edit1.Text := FormatFloat('0.00', value - UpDown1.Increment*0.01);
  12. end;
   

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: TUpDown increment type
« Reply #5 on: May 01, 2019, 11:18:40 am »
Do not link the UpDown to the Edit (i.e., the Associate of the TUpDown must remain empty) to avoid the UpDown changing the Edit on its own. Then write this event handler for the OnClick event of the UpDown:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
  2. var
  3.   value: Double;
  4. begin
  5.   if not TryStrToFloat(Edit1.Text, value) then
  6.     MessageDlg('No number', mtError, [mbOK], 0)
  7.   else
  8.   if Button = btNext then
  9.     Edit1.Text := FormatFloat('0.00', value + UpDown1.Increment*0.01)
  10.   else
  11.     Edit1.Text := FormatFloat('0.00', value - UpDown1.Increment*0.01);
  12. end;


Thanks wp, that's a far more elegant solution than I'd come up with. I realised the answer was to not associate the TEdit.
Shame the TUpDown can't have a increment value separate from the UpDown count.

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: TUpDown increment type
« Reply #6 on: May 01, 2019, 11:40:10 am »
A limitation of this solution may be that the Min and Max of the UpDown are Integers and, after division by 100, may become too small for your real values (have fun clicking through 1 million dollars in 1-cent-steps, BTW  ;)).

Here is another solution based on TFloatSpinEditEx (on the LazControls palette) which is a combined control of TEdit and TUpDown. The UpDown being a protected property can be accessed after the usual hacking type-cast:

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyFloatSpinEditEx = class(TFloatSpinEditEx);
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. begin
  6.   with TMyFloatSpinEditEx(FloatSpinEditEx1).UpDown do begin
  7.     Orientation := udHorizontal;
  8.     Width := 2*FloatSpinEditEx1.Height;
  9.   end;
  10. end;

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: TUpDown increment type
« Reply #7 on: May 01, 2019, 12:03:58 pm »
Thanks again.
I'll have a play when I get home from work.

The end goal is the ability to create or find profit on return from cost or retail price etc. The UpDown allows for fine tweaking of margins etc. to a nice looking price.

I made something fairly similar in D3 many years ago.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: TUpDown increment type
« Reply #8 on: May 06, 2019, 07:24:41 pm »
A limitation of this solution may be that the Min and Max of the UpDown are Integers and, after division by 100, may become too small for your real values (have fun clicking through 1 million dollars in 1-cent-steps, BTW  ;)).

I was just fixing my UpDown controls due to a Cocoa bug, and, for the record, I do use UpDown as wp suggests, taking the float value in an Edit, and increment or decrement it by a float in UpDown OnClick...  :)
« Last Edit: May 06, 2019, 07:29:14 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

LeeJiEun

  • New Member
  • *
  • Posts: 17
Re: TUpDown increment type
« Reply #9 on: May 08, 2019, 10:47:22 pm »
(have fun clicking through 1 million dollars in 1-cent-steps, BTW  ;)

I needed a control to select values for an electronics program, and the values needed to be adjustable over a very wide range. What I came up with was something similar to a thumbwheel switch, there is an UpDown for each digit, the values from the UpDowns are combined by multiplying each by a power of 10 and then adding the results together, then that number is used in calculations and also converted to a string and displayed as a label on a panel above the buttons.

Image attached.

 

TinyPortal © 2005-2018