I’ve got a canvas subclass instantiated on a window. In the constructor of the subclass, I would like to initialize some other properties based on the width and height of the canvas. The width and height properties return zero, but that’s not true based on the GUI.
I’m not overriding the constructor, just using the one defined within the subclass.
Is this normal? How do you work around this?
Also, the default value of some properties does not seem to stick. For example, a value is preset to 25 and after instatiation, it’s value is 10, and nowhere do I change the value via code.
Xojo 2018 R1.1
Use the Open even instead of the Constructor.
The Constructor is called before the control is fully initialized. Use the Canvas.Open event for any code that expects the canvas to be ready to go.
Thanks @Beatrix Willius and @Andrew Lambert
How about default property values not staying at their default value?
Are they computed properties whose initial value was set in the IDE’s property Inspector? If so, they receive their initial values after the Constructor returns (but before the Open event.)
They are regular properties(not computed, not shared). The default value is set in the IDE in the inspector.
I am monitoring the value in the constructor, open event, and afterwards.
It changes value between the constructor and open event.
Inspector values are only copied across as you drag the instance onto the window. If you change the Super’s values in the Inspector after its been added to a window, the changes are not changed for the control on the window.
I just figured that out. I deleted the control, and re-dragged/instantiated it back into the window…The parameter value changed, but it’s still not matching the super. It’s bizarre.
[quote=394854:@Chad Posner]They are regular properties(not computed, not shared). The default value is set in the IDE in the inspector.
I am monitoring the value in the constructor, open event, and afterwards.
It changes value between the constructor and open event.[/quote]
These blog posts may help:
https://blog.xojo.com/2018/01/11/control-subclass-constructors/
https://blog.xojo.com/2017/03/13/inspector-behavior-class-defaults-from-the-xojo-ide/
https://blog.xojo.com/2018/05/07/create-your-own-imagewell-based-on-canvas/
This is not a bug - its a relatively little understood feature of how its possible to have one super class and then have lots of instances that have different initial values.
Imagine you had a “button” class and the only way to set values was in the Super class. Everybutton would have the same caption, height, width, etc. Thats not useful.
The inspector behaviour is the “default” set of values that each instance will get when you add them to a layout - as the layout is created each instance is created and initialized with code that is a lot like:
dim ctl as New <controlType> // <<<< your constructor is called here
ctl.SetPropertiesToInitialValuesFromDesign ( ... ) // and each instances properties are set here possibly
// overriding whatever you set in the constructor
Now you can have many control instances each with varying properties at runtime all derived from the same superclass.
You set the “default set of instance values” using the Inspector Behaviour panel.
That’s sort of right. You can control the initial configuration of each instance as it’s added but then if you change the values in the inspector - those are the values that will get used at runtime. When you think of it that’s exactly what you’d expect to happen.