Recent

Author Topic: Which Control should I use ?  (Read 2015 times)

wp

  • Hero Member
  • *****
  • Posts: 13630
Re: Which Control should I use ?
« Reply #30 on: June 14, 2026, 05:58:33 pm »
Why don't you put the functionality of this panel into a separate form? This would remove some code from that heavily crowded main unit, allow the user to drag the form (=panel) to another place if its default position would cover interesting parts of the form, and would fix the issue that when the panel is displayed, the "bold" checkbox is focused, and you press TAB, the focus leaves the panel and goes to the "Decimal places" edit box in the main form.

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #31 on: June 14, 2026, 07:23:30 pm »
Interesting point @WP.  To date I have never used more than one 'Form' in any program.  A more likely idea might be to make the whole 'Printing parameters' a stand alone 'Unit' which might well compliment my Date/Time Unit . . . . . but that's very much for another day (week?).

I still have some other parts of the main program to write (specifically the drawing of dimension lines & dimensions when 'Print Image' is requested) so I may well look at that option in future. This Project currently has ~7.5k lines - my 'Accounts' program source is 17K+   ::)

I've now commented out the 4 lines of @paweld's suggestion that I queried and have yet to see any adverse effect.

I've added the SelLength .. code to the end of each Font edit controls and anywhere that the FontNames TComboBox is addressed and the only time that I get the blue background is upon a change of selection !  RESULT

Using the OnDropDown Event I can also change the font face to an easily readable face - rather than it being in the previously set 'Default Face' which could be difficult to read under some circumstances.

Overall a very good day - better still, I learned a little while ago that Lewis Hamilton has won the Spanish Grand Prix !!  My next 2½ hours will be watching how he did so  :D

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Aruna

  • Hero Member
  • *****
  • Posts: 814
Re: Which Control should I use ?
« Reply #32 on: June 14, 2026, 09:44:50 pm »
Hello J-G,

Please try the attached ZIP file. It calculates the distance between two points. I’m not a mathematician, so I asked ChatGPT for help and was pleasantly surprised by the solution it suggested and generated. I also ended up learning quite a bit about geometry in the process 😄

ownerdraw by the way is actually rather simple once you know what to do.

Minimal OwnerDraw Requirements
1. Set OwnerDraw style
Code: Pascal  [Select][+][-]
  1.     ComboBox1.Style := csOwnerDrawFixed;

2. Handle OnDrawItem event
Code: Pascal  [Select][+][-]
  1.    procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);

3. Minimal drawing code
   
Code: Pascal  [Select][+][-]
  1.  procedure TForm1.ComboBox1DrawItem(Control: TWinControl;  Index: Integer; ARect: TRect; State: TOwnerDrawState);
  2.    var
  3.   CB: TComboBox;
  4. begin
  5.   CB := TComboBox(Control);
  6.  
  7.   CB.Canvas.FillRect(ARect);
  8.   CB.Canvas.TextOut(ARect.Left + 2, ARect.Top + 2, CB.Items[Index]);
  9. end;

That’s it
If you have these 3 things:

csOwnerDrawFixed
OnDrawItem
FillRect + TextOut

✔ it works
✔ no extra code required
« Last Edit: June 14, 2026, 10:02:35 pm by Aruna »

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #33 on: June 14, 2026, 11:44:01 pm »
Hello J-G,

Please try the attached ZIP file. It calculates the distance between two points. I’m not a mathematician, so I asked ChatGPT for help and was pleasantly surprised by the solution it suggested and generated. I also ended up learning quite a bit about geometry in the process 😄

Hi Aruna,

If my Triangle program has prompted you to look seriously at Mathematics - and/or Geometry/Trigonometry I am well pleased :)

The reason I started this project was because my Grandson told me that he had never done ANY Trigonometry at school so knew nothing about Sin, Cos, Tan etc. so this is to give him a grounding.

The distance between the centres of the Inscribed & Circumscribed circles of a Triangle is really of little interest, and in fact the formula for such is pure Pythagoras - A2 + B2 = C2. What is much more important is the lateral distance between them  ie. the length of A & B.  These are used to plot the circles either on screen or indeed on paper. Your project does very well for a 400 ish line program but of course the majority of trigonometry problems do not have all three sides known, hence why my program allows input of any three dimensions (though there must be at least one side known).

I've taken things much further, purely for the pleasure of solving problems !  -  hence why I've included quick methods to modify the input or calculated results since solving Triangles is often a 'stepping stone' to answer the real question.

Do keep up your research into mathematics - there is a wealth of facinating material - not just about simple triangles, look up Reuleaux triangles (and Polygons) and Conic Sections  :o

I will try to investigate OwnerDraw but not this week !



« Last Edit: June 14, 2026, 11:47:18 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

paweld

  • Hero Member
  • *****
  • Posts: 1685
Re: Which Control should I use ?
« Reply #34 on: June 15, 2026, 06:49:32 am »
@J-G: Sorry, my English isn't the best, so I just posted the code with a brief comment.

Let me explain why I call the “*M_EditEditingDone” methods when displaying the print configuration panel - as you can see in the attached GIF, after the panel is displayed, the visual representation of the margins does not match their actual values (toggling between controls without changing their values updates the visual representation).
And why do I call it with the nil parameter? Because you don’t use the Sender parameter in these methods, so it doesn’t matter what parameter value is passed.
Best regards / Pozdrawiam
paweld

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #35 on: June 15, 2026, 09:34:46 am »
@J-G: Sorry, my English isn't the best, so I just posted the code with a brief comment. [...]
Many thanks for the further input @paweld -  I must say that your English is vastly superior to my command of your native language !! - whatever it is  :D

It is good to know the thinking behind your suggestion at least and of course it now makes sense.

The code that I posted was prior to me addressing (correcting) the margin visualization. Originally I had assumed that printing would only be done to A4 paper (210 x 297mm) in 'Portrait' Mode and the 'PaperSize' image was fixed at 105 x 148 pixels. Then I added the option to have 'Landscape' and later still the options of A6, A5 & A3.

What I hadn't realized at the time was that the calculation of the displayed margin size was ONLY for A4  - that is, I divided the margin in mm by 2 and took that off the paper size using this code :
Code: Pascal  [Select][+][-]
  1.   prMarTp := pgMarTp DIV 2;

I have now introduced 4 more variables for the display Margins and a PageIndex so that code has expanded to :
Code: Pascal  [Select][+][-]
  1. procedure CalcDisplayMargins;
  2. var
  3.   D : single;
  4. begin
  5.   case pgIndex of
  6.     1 : D := 1;
  7.     2 : D := 1.414;
  8.     3 : D := 2;
  9.     4 : D := 2.828
  10.   end;
  11.   dMarTp := trunc(pgMarTp / D);
  12.   dMarBt := trunc(pgMarBt / D);
  13.   dMarLt := trunc(pgMarLt / D);
  14.   dMarRt := trunc(pgMarRt / D);
  15. end;
  16.  
This takes account of the physical paper size so gives a better indication to the user of the real amount of space available for printing.

I've also changed the class of the PaperSize & P_Margins components to TShape rather than TLabel & TImage thereby adding an outline.

Attached are two more screen grabs of what is now displayed when the paper size selected is A6 P or A3 L  - both with the same margins. I'm sure you'll see the difference.  The size of the TShape remains at 105 x 148 in all cases simply having Width & Height swapped to suit.
« Last Edit: June 15, 2026, 04:45:33 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #36 on: June 15, 2026, 06:54:01 pm »
Having mentioned Reuleaux Triangle in a reply to @Aruna yesterday, I found myself in yet another branch of the Rabbit Warren today - adding just such to the display, when warranted of course.

This can only be when the triangle is Equilateral so I didn't have much checking to do but I did wander down paths that were destined to be dead ends !  %) 

I've attached a screen grab of the result, in case anyone is not familiar with the notion of a curved Triangle. The important aspect is that rather than referring to the 'Length of side' they are specified by 'Diameter' since, just as with a circle, the measure across any part of the figure is the same.

To enable drawing of this I had to explore parts of Lazarus that I haven't had to previously - namely Arc and AngleArc.   ARC gave me results I didn't expect (though interesting) which is why I looked at ANGLEARC. This does give what I need but there is a peculiarity that I didn't anticipate.

I only need a dashed line from each vertex to the next but AngleArc also draws a line across the 'chord'. It was simple matter of drawing the AngleArc before drawing the Triangle line (which needs to be solid).  This seems 'wrong' but I'm sure that whoever wrote the AngleArc routine considered the matter and it may well be that there is a 'flag' that I could set to prevent such - I haven't found one as yet.

The second attachment shows what I'm sending to the routine - Centre (X,Y), Radius, start position ° & sweep °.

Am I missing a flag - such as 'closed' - or is there yet another routine that draws an 'open' Arc ?

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #37 on: June 15, 2026, 07:24:59 pm »
Why don't you put the functionality of this panel into a separate form?
Taking this idea 'on board' I've added a [New Form] with the hope that I might be able to just re-assign the [Parent] of the MTF_Panel.  I have done this very easily in the past when I've deemed it more convenient to have similar controls grouped on a TPanel. However, selecting the MTF_Panel and then change Parent the list of optiones doesn't include the new Form.

I'm obviously remiss in my knowledge :(  and it is not the simple matter that I hoped;  do I have to re-create The MTF_Panel from scratch on the New Form ?   or is there some 'editing magic' - such as changing TTriangleSolution. to the name of the new form.  (I think unlikely   ::)

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

wp

  • Hero Member
  • *****
  • Posts: 13630
Re: Which Control should I use ?
« Reply #38 on: June 15, 2026, 08:19:03 pm »
There are several procedures related to arcs - the attached projects uses all of them (well, except ArcTo which is like Arc but moves the current point).

wp

  • Hero Member
  • *****
  • Posts: 13630
Re: Which Control should I use ?
« Reply #39 on: June 15, 2026, 08:25:37 pm »
do I have to re-create The MTF_Panel from scratch on the New Form ?
Select the panel in the old form, copy it to the clipboard and paste it into the new form. However this does not copy the event handlers, which you must recreate in the new form (but of course you can copy the event handler code to the new form).

One problem that I am expecting in separating the panel from the main form is that you'll easily drift off into a circular reference of units: Mainform uses Panelform, and Panelform uses Mainform. To resolve this try to add the unit in the implementation section to the uses clause.

Aruna

  • Hero Member
  • *****
  • Posts: 814
Re: Which Control should I use ?
« Reply #40 on: June 16, 2026, 12:34:15 am »
Why don't you put the functionality of this panel into a separate form?
Taking this idea 'on board' I've added a [New Form] with the hope that I might be able to just re-assign the [Parent] of the MTF_Panel.  I have done this very easily in the past when I've deemed it more convenient to have similar controls grouped on a TPanel. However, selecting the MTF_Panel and then change Parent the list of optiones doesn't include the new Form.

I'm obviously remiss in my knowledge :(  and it is not the simple matter that I hoped;  do I have to re-create The MTF_Panel from scratch on the New Form ?   or is there some 'editing magic' - such as changing TTriangleSolution. to the name of the new form.  (I think unlikely   ::))

1 - File --> New Form
2 - Add the code below to the FormShow event in unit Tri:
Code: Pascal  [Select][+][-]
  1. procedure TTriangleSolution.FormShow(Sender: TObject);
  2. begin
  3.   Output_Panel.Parent := Form1;
  4.   Output_Panel.Align := alClient;
  5.   Form1.Show;
  6. end;

3 - Run the project.
4 - Adjust, adapt, modify as you see fit.

Screenshot attached.                         
« Last Edit: June 16, 2026, 04:46:48 am by Aruna »

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #41 on: June 16, 2026, 02:30:55 pm »
Apologies for the delay in responding  -  'Life' sometimes demands it's own time !

Thanks both for your input, as you may well imagine I haven't progressed very far - hence more questions.

@Aruna - You've slightly mis-understood which Panel I'm looking to move to a 'Form'. It is the MTF_Panel rather than the Output_Panel. This just meant that I had to assess what you were suggesting and modify the 'names' accordingly. No great shakes but better that you are fully informed.  The 'Output_Panel' holds the buttons that affect how program output is dealt with (Data To Spread-Sheet or Printer; Image to Printer, along with which device is to be used)  The MTF_Panel is the means by which the 'Device' is configured;  This is the one that @WP suggested might be better as a 'Form'.

The new form is called 'Pr_Form' and the controlling unit 'Print'. 

I have succesfully 'copied' the MTF_Panel to the new form but as @WP mentioned the events are not copied and I can see that it's mearly a case of re-creating them with the correct headers - copying the code for each event will be a trivial excercise, it just has to be done!

As yet, I cannot determine whether @Aruna's code snippet means that I don't need to have the MTF_Panel actually on Pr_Form or not and I have not yet been able to compile the project to find out.

Adding a FormShow event to Tri - thus :
Code: Pascal  [Select][+][-]
  1. procedure TTriangleSolution.FormShow(Sender: TObject);
  2. begin
  3.   MTF_Panel.Parent := Pr_Form;
  4.   MTF_Panel.Align:=alClient;
  5.   pr_Form.Show;
  6. end;
  7.  
made the compiler complain that 'Pr_Form' could not be found. ( I was asked (and responded positively) to save the new Unit)

Adding Pr_Form to the Uses Clause of Tri (Interface section) had a slightly different error but still a 'not found'. Since there isn't a Uses section in the Implimentation Section, I created one and added the Pr_Form:
Code: Pascal  [Select][+][-]
  1. var
  2.   TriangleSolution: TTriangleSolution;
  3.  
  4. implementation
  5.  
  6. {$R *.lfm}
  7. Uses
  8.   Pr_Form;
  9.  
Again the compiler reported 'Cannot Find'.

I have checked and Print.pas & Print.lfm are both in the same folder as Tri.Pas.

So - for now, I'm stumped as to how I can get Pr_Form to be recognised as a valid Form  :(


« Last Edit: June 16, 2026, 03:58:24 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

wp

  • Hero Member
  • *****
  • Posts: 13630
Re: Which Control should I use ?
« Reply #42 on: June 16, 2026, 05:08:55 pm »
Not seeing you code, I would summarize the entire operation like this:
  • create a new form in a new unit
  • First decision: What will be the name of the unit, what will be the name of the form class and the form itself? They must be different! It is helpful to use a "prefix" naming system (begin the unit name with a "u" or an appreviation of your project ("tri", maybe)), or simply add "unit" to the name. Class names should always begin with a "T". So, when the form is to be named "Pr_Form", the form class will be named "TPr_Form", and the unit should be saved as "tri_pr_form" (for example) - this defines the unit name.
  • copy the MTF_Panel over to the tri_Pr_Form unit and integrate it into the TPr_Form, recreate the event handlers.
  • In order to later center the Pr_Form over the main form, you could set the property Position of TPr_Form to poMainFormCentered
  • in your mainform unit "tri" add unit "tri_pr_form" to the uses clause in the implementation section (don't use the interface section, it will probably create a circular reference)
  • in the OnClick event of the control which must be clicked to display the MTF_Panel simply add "Pr_Form.Show". This works because the instance Pr_Form of TPr_Form is created automatically by default. No setting of parent because you want the form to be floating above the main form, and also no "Align", as Aruna suggested.
  • when the Pr_Form needs to access elements from the main form add unit "tri" to the implementation's uses clause of tri_pr_form.
  • There are more reasons that compilations still may fail here. I could imagine that the MTF_Panel accesses the type declarations that you have in the implentation of unit "tri". But unit "tri_pr_form" does not see them just for this reason. You could move them into the interface part of the "tri" unit. I think this fixes this issue, but the more you have in the interface the higher the risk of a circular reference. Better: Create yet another unit for the types needed by your program, maybe unit "tri_types". Use this in the implementation uses of "tri" and "tri_pr_form".
I hope you have a backup copy of your original program before you began these changes. There is always a risk that refactorings may fail.

J-G

  • Hero Member
  • *****
  • Posts: 1114
Re: Which Control should I use ?
« Reply #43 on: June 18, 2026, 10:18:47 am »
Thanks for that exposé of the potential 'issues' @WP, as I have found in the past, your advice has usually been extremely helpful - nay. has often Solved whatever ailed me :D

I have various sub-folders in my 'Lasarus' folder whose names commence 'Ex-' ie. 'ExWP' is one (as is 'ExAruna') and your list is by far the largest and goes back to 2017!

Yesterday I had a fallow day as far as Pascal is concerned - I had some sheet music to create - so I had time to reflect on my options. In the light of your summerization, I decided that the most sensible aproach would be to 'back-track' on the few changes I'd made - such that compilation was possible -  and start afresh. 

Incidentally, your reference to 'Backup' is very well founded, I fell into that trap many years ago and did a lot of research on how Lazarus deals with the matter (very well as it happens) and my [backup] folder for the Triangle project has > 8.3k items (875Mb) with Triangle.lps;2125 being the latest entry  :o  -  not that I'm paranoid and I'm sure I could safely delete most of those files :D

To this end I tried to [Delete] the form I'd created but even though that option is listed in the right-click Menu, it is ineffective.  I was able to delete the copy of the MTF_Panel so that the 'form' was empty though and after removing redundant references 'Triangle' compiled.

The new form (blank) simply appears and I don't doubt that I can find a way to [Hide] it.

As you surmise, there are many type and variable declarations in Unit 'Tri' upon which the MTF_Panel relies. With this in mind, I've come to the conclusion that moving to a form - at this stage - is probably more trouble than it's worth. HOWEVER,  now that I see the potential benefits of separate 'floating' forms for such functions I will certainly look at the idea for whatever my next project is.

The fact that some years ago I created my Date-Time Unit prompts me to consider doing something similar with the code for the MFT_Panel, that is, a 'Printer_Setup' Unit.

In this particular case, your observation that the MTF_Panel covers part of the main display is somewhat irrelevant since this panel would only normally be used when there is nothing else of any import to see -  which is another reason to 'leave well alone'  ;D

« Last Edit: June 18, 2026, 10:40:30 am by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

wp

  • Hero Member
  • *****
  • Posts: 13630
Re: Which Control should I use ?
« Reply #44 on: June 18, 2026, 11:00:01 am »
The new form (blank) simply appears and I don't doubt that I can find a way to [Hide] it.
A freshly created form is not displayed normally, it has "Visible=false". But your code in reply #41 shows that you have an OnShow event handler for TTriangleSolution which explictly shows pr_Form. Remove this handler, it is not needed.

 

TinyPortal © 2005-2018