Recent

Author Topic: [NEWBY] Trying to undestand of a control that is buried quite deep on a form  (Read 705 times)

tfurnivall

  • Jr. Member
  • **
  • Posts: 80
I have an image (TImage) which I am trying to get to on a form (see attached project Syntax Charts).

As far as I can see from the Object Inspector the hierarchy is as follows:
Code: Pascal  [Select][+][-]
  1. SDLModelForm   : TSDLModelForm;
  2.     mpWorkBench : TPageControl;
  3.         pgChart      : TTabSheet;
  4.         imgPaint     : TImage;
  5.        
When I try and reference the image I am using - as a first attempt -
Code: Pascal  [Select][+][-]
  1. SDLModelForm.mpWorkbench.pgChart.imgPaint.canvas
  2.  
to get at the components of the canvas associated with img1 (associated etc back up to SDLModelForm).

I get an error on pgChart (line 152 of the ModelForm code) which basically says No such thing.

So I then try indexing the pages on the mpWorkBench (pgChart is page 0, and pxPaint is defined as 0):
Code: Pascal  [Select][+][-]
  1. SDLModelForm.mpWorkbench.pages[pxChart].imgPaint.canvas
This time the error comes on imgPaint - same squawk - No such object. However, the compiler seems to accept pages[pxPaint] with no problem.

So my question is how do I spell out the qualifications for imgPaint?

Tony

dseligo

  • Hero Member
  • *****
  • Posts: 1601
How about: SDLModelForm.imgPaint.canvas

dseligo

  • Hero Member
  • *****
  • Posts: 1601
After I wrote my answer I noticed that you included code.
You have an error there, there is no declaration of imgPaint in your form class - you probably deleted it (cause it should be added by Lazarus when you drop component on the form).
You can add line
Code: Pascal  [Select][+][-]
  1.      imgPaint: TImage;
below pnlNavigation: TPanel; in TSDLModelForm declaration.

tfurnivall

  • Jr. Member
  • **
  • Posts: 80
Curiouser and curiouser. When I use F12 (swicth between Form and Code), that's exactly the result I get.

But

When I use F1`1 to get at the Object Inspector, imgPaint is there - exactly where it should be.

F12 - Source switching
Code: Pascal  [Select][+][-]
  1.     TSDLModelForm = class(TForm)
  2.         chkMouseClick: TCheckBox;
  3.         chkMouseMove: TCheckBox;
  4.      cmdAction: TButton;
  5.      cmdLast: TButton;
  6.      cmdNext: TButton;
  7.      cmdFirst: TButton;
  8.      cmdPrev: TButton;
  9.      cmdPrintForm: TButton;
  10.      cmdClearRefresh: TButton;
  11.      cmdExit: TButton;
  12.      cmdDelete: TButton;
  13.      lblSegmentList: Tlabel;
  14.      Mainmenu1: Tmainmenu;
  15.      Menuitem1: Tmenuitem;
  16.      mnuEditCut: Tmenuitem;
  17.      mnuEditPaste: Tmenuitem;
  18.      mnuEditCopy: Tmenuitem;
  19.      mnuHelp: Tmenuitem;
  20.      mnuSettings: Tmenuitem;
  21.      mmSaveAs: Tmenuitem;
  22.      mnuEdit: Tmenuitem;
  23.      mmFile: Tmenuitem;
  24.      mmOpen: Tmenuitem;
  25.      mmExit: Tmenuitem;
  26.      Separator1: Tmenuitem;
  27.      Label1: TLabel;
  28.      lblMouseAdjustment: TLabel;
  29.      lblDebugInfo: TLabel;
  30.      lblLogonTimer: TLabel;
  31.      lblStatusLine: TLabel;
  32.      mpWorkbench: TPageControl;
  33.      pgChart: TTabSheet;
  34.      pgConfig: TTabSheet;
  35.      pgEnvironment: TTabSheet;
  36.      pgTools: TTabSheet;
  37.      pgOptions: TTabSheet;
  38.  

F11 Hoping I can get the Object Inspector to copy

No - I can't. It is however the hierarchy that I wrote out in the original post:
Code: Pascal  [Select][+][-]
  1. SDLModelForm   : TSDLModelForm;
  2.     mpWorkBench : TPageControl;
  3.         pgChart      : TTabSheet;
  4.         imgPaint     : TImage;
  5.  

So my (very naive question) becomes

Where the h* is Object Inspector getting its data?

T

n7800

  • Hero Member
  • *****
  • Posts: 542
@dseligo, look at this line of code:

As far as I can see from the Object Inspector the hierarchy is as follows:
Code: Pascal  [Select][+][-]
  1. SDLModelForm   : TSDLModelForm;
  2.     mpWorkBench : TPageControl;
  3.         pgChart      : TTabSheet;
  4.         imgPaint     : TImage;
  5.        
When I try and reference the image I am using - as a first attempt -
Code: Pascal  [Select][+][-]
  1. SDLModelForm.mpWorkbench.pgChart.imgPaint.canvas
  2.  

it seems the author thinks he should replicate the entire component hierarchy from the Object Inspector...

n7800

  • Hero Member
  • *****
  • Posts: 542
@tfurnivall, the Object Inspector simply shows a "graphical hierarchy" used to draw components. This is unrelated to the hierarchy of "entities" in the code. In case you're wondering, it reads the "Parent" property of components (invisible in the Inspector).

As you can see in the Pascal code, a form is simply a class ("TSDLModelForm = class(TForm)"). And each component added to the form is added to this class as a field. Therefore, to access any component in the form, you simply write the component's name:

Code: Pascal  [Select][+][-]
  1. imgPaint.Canvas

So, component "Name" must be unique for the entire form. But this allows you to move components around within the form without having to change the code.
« Last Edit: September 19, 2025, 12:16:57 am by n7800 »

n7800

  • Hero Member
  • *****
  • Posts: 542
By the way, to access the image in a TImage, need to use the Picture property, for example:

Code: Pascal  [Select][+][-]
  1. imgPaint.Picture.LoadFromFile('image.png');

The Canvas property is used for other purposes, and only in the OnPaint event of this component. Anyway, that's a whole other topic...

dseligo

  • Hero Member
  • *****
  • Posts: 1601
Where the h* is Object Inspector getting its data?

He's getting data from form (which is in file modelform.lfm, in your project).

But it has to have declaration in *.pas file also, which you are missing.

I corrected code and attached it here. It compiles now, but I didn't try to run it (I didn't analyze what your code should do).

dseligo

  • Hero Member
  • *****
  • Posts: 1601
As you can see in the Pascal code, a form is simply a class ("TSDLModelForm = class(TForm)"). And each component added to the form is added to this class as a field. Therefore, to access any component in the form, you simply write the component's name:

Code: Pascal  [Select][+][-]
  1. imgPaint.Canvas

Exactly. And if he wants to access component from code outside form (not from methods of this form), he can do it by putting form name in front like this (it works because forms are global variables):
Code: Pascal  [Select][+][-]
  1. SDLModelForm.imgPaint.canvas

tfurnivall

  • Jr. Member
  • **
  • Posts: 80
OK, I've had a little time to work on some aspects of this. First some background.

The form came from another project (Graphical Workbench) which I use for exploring how to do things in a graphical environment. IIRC I simply copied modelform.pas and modelform.lfm and pasted them into the directory for Syntax Charts (I suspect that this was the root cause of my problems). I then systematically removed pages of the multi-page control, until most of the 'working' pages were gone. Even though the pages were removed and saved, and seemed to return the modified version if I got out of Lazarus and reopened the Syntax Charts project, I do not know (and certainly don't understand) where the file was stored in between times. It's as if the IDE incorporates this file but then ignores it.

So I'm going to recreate all this mess using a different project which I'll then bundle up as I make the changes.

BTW I am trying to use MouseDown, MouseMove and Shift to constrain the drawing of lines that will always be vertical or horizontal. I'd gotten to the point in (Graphical Workbench) where this was producing some visibly consistent results. So, yes, I'm working directly on the Canvas, and am nowhere near ready to save it out to a file - or bring it back from said file. I'm just putting bits onto the canvas.

So my next post will include the original Graphical Workbench and TestProject1. Give me a couple of hours to get it all set up, and then I'll be back.

Note for people who just happen on this thread - it's very likely going to get rather tedious as I go through the Experiment - Review - Export - Comment cycle. I appreciate those who follow me along this path - and I hope it's worth it for me, in terms of enlightenment and understanding!

T

 

TinyPortal © 2005-2018