Recent

Author Topic: How to replace Delphi TSpinButton?  (Read 1321 times)

BosseB

  • Sr. Member
  • ****
  • Posts: 468
How to replace Delphi TSpinButton?
« on: September 21, 2020, 07:22:33 am »
I am porting an old application from Delphi to FPC/Lazarus.
It turns out to contain a number of controls named TSpinButton, which is among the "Samples" controls in Delphi 2007.
But Lazarus does not recognize this out of the box, so it throws a lot of errors when I try to view the form in Lazarus.

Is there a package containing the corresponding control in Lazarus such that I can actually load the form and edit it?
For example a "DelphiSamples" package that enables me to port these applications?

I have found a similar looking control in Lazarus named TUpDown but it is not the same, it lacks the events OnDownClick and OnUpClick, which are used by the Delphi program.
The TUpDown component has just the OnClick event available...

I am prepared to hand edit both the pas and dfm files to correct this problem but I don't know what to do about the missing events...
Do I have to create my own TSpinButton by inheriting from TUpDown and adding the missing events? If so how can that be done?
--
Bo Berglund
Sweden

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to replace Delphi TSpinButton?
« Reply #1 on: September 21, 2020, 08:30:46 am »
You can mimick those events with a handler for the event OnChangingEx; it has a parameter which tells you in which direction it's changing, whether up or down, so you can call your OnUpClick or OnDownClick handlers as if they were "normal" methods (which indeed they are ;) ). Or integrate their code in the handler for OnChangingEx, if you prefer.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: How to replace Delphi TSpinButton?
« Reply #2 on: September 21, 2020, 09:47:14 am »
I have made a quick test now and I could replace the functionality of TSpinButton with TUpDown but I also need to put a case statement in the OnClick event for the component where the actions are selected:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
  2. begin
  3.   case Button of
  4.     btNext:
  5.       begin
  6.         if HVIndex = 255 then exit;
  7.         Inc(HVIndex);
  8.         Edit1.Text := FormatFloat('0', HVIndex * 1.6);
  9.       end;
  10.     btPrev:
  11.       begin
  12.         if HVIndex = 0 then exit;
  13.         Dec(HVIndex);
  14.         Edit1.Text := FormatFloat('0', HVIndex * 1.6);
  15.       end;
  16.   end;
  17.  
This does what it is supposed to do but involves a lot of changes that cannot easily be done as global replaces...
But it might not be avoidable...

In order for it to work seamlessly I would have to create a child of TUpDown where the  two events are added (don't know how to do this) and also somehow integrate this into Lazarus so it becomes part of the component palette. This way future ports of other similar applications would be simpler.
OTOH, then the special component package would have to be installed whenever I set up a new FPC/Lazarus environment (happens frequently).
I guess no matter what I do I will have a problem...

Edit:
In the code example above I pulled in the code from the original 2 events, but I might also just call these respectively:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
  2. begin
  3.   case Button of
  4.     btNext: spbMaxVoltageUpClick(Self);
  5.     btPrev: spbMaxVoltageDownClick(Self);
  6.   end;
  7. end;
  8.  
« Last Edit: September 21, 2020, 09:53:02 am by BosseB »
--
Bo Berglund
Sweden

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to replace Delphi TSpinButton?
« Reply #3 on: September 21, 2020, 11:46:44 am »
If your project contains dozens of TSpinButtons it is easiest do write a unit with a TSpinButton for Lazarus. It is not difficult, and I am attaching a package "DelphiSamples" which in spite of its name contains only a TSpinButton (maybe it will be extended later).

You should be aware that this SpinButton inherites from TCustomUpdown and implements only the FocusControl property and the OnDownClick and OnUpClick events. The other DownGlyph and UpGlyph properties are not considered because TUpDown cannot change them in any way.

Note also that the unit had to be renamed - it is called Samples_Spin instead of Spin -, because there is already a Spin unit in the standard Lazarus installation. This means that you have to touch every Delphi unit with a TSpinButton and replace this unit name (if you work with the automatic converter you can try to add a rule, but you must know that the Lazarus unit Spin contains components with are included in the Delphi spin. So there may be cases in which you need both, Spin and Samples_Spin.

You must install the provided package ("Package" > "Open Package File" > navigate to "DelphiSamples.lpk" > Click "Install" > "Use")

If your project contains only a few TSpinButtons the requirement to install a package that you do not need for anything else is a disadvantage, and it may be more appropriate to approach the issue fromt the Delphi side: In all forms containing a TSpeedButton I'd add a TUpDown, adjust its properties to the TSpinButton and write its onClick event handler to do the same as the OnDownClick and OnUpClick events of the TSpinbutton. Then I'd delete the SpinButton. This way you have a working Delphi application and you need not care about this component any more in the conversion.

 

TinyPortal © 2005-2018