Andrew Spencer 2 Posted November 27, 2023 I've often used a derived class on TCustomControl to access a control's Canvas property. type TGetCanvas = Class(TCustomControl); and then used it later in a line similar to this: TGetCanvas(progressPanel).Canvas.TextWidth("Hello World") In trying to neaten things up, I noticed that the same type declaration was in the interface or implementation section of numerous units. I tried moving the declaration to the interface section of a "library" unit, and useing that unit in all these other units. On compilation, however, a line like the one above would raise and error "E2362 Cannot access protected symbol TCustomControl.Canvas". Why is this not functioning in the same way as if the type declaration was in the unit itself? Share this post Link to post
dummzeuch 1506 Posted November 27, 2023 11 minutes ago, Andrew Spencer said: Why is this not functioning in the same way as if the type declaration was in the unit itself? That's for historical reasons. And with the "new" strict keyword even this can be prevented. If you only want to access the Canvas property you can change your TGetCanvas class to this: type TGetCanvas = Class(TCustomControl) published property Canvas end; This is used by all the classes that derive from TCustomControl to publish protected Canvas property, so it will always work. 1 Share this post Link to post
Lajos Juhász 293 Posted November 27, 2023 A warning Canvas.TextWidth is valid only during painting, otherwise it's not guaranteed that the canvas has the correct font assigned. To be sure you can use https://docwiki.embarcadero.com/Libraries/Sydney/en/Vcl.Graphics.TCanvas.RequiredState. 1 Share this post Link to post