Custom control

Hi,
I’ve created a custom control, a list box, and I placed it into the main window.
I wrote a new constructor for this control, in order to initialize the number of columns.
I noticed that I have to change them visually using the Inspector.
I am wondering if I can change by default some aspects of the new control using the class and not the Inspector every time for a new instance.

This is what the CONSTRUCTOR is for…

Thank you Dave for your response, but this is what I mentioned before:

It could be that the control is updated by the superclass after the constructor. Perhaps ask @Greg O’Lone

Make several subclasses, one for each variation, and set the number in each subclass constructor.

Though that seems a bit inelegant.

I would add a custom property DefaultNumberOfColumns as integer to the class, and make it accessible in the IDE so it is easily set. Depending on the value you can change what you want in the constructor (?) or Open event.

I stand by this.

myObject = new customObject(14)

pass the value to the Constructor…
OR do as Markus indicated and have multiple hardcoded values… .up to you

  • use the Inspector
  • use the Constructor
  • place it in the OPEN event
  • make multiple subclasses

This is what I am suspecting… but is this a correct behavior?

There are no variations. All I need is to create a list control specialized to display contents from a specific table of a database.
The list control will be the main viewer in the application’s window.

Perhaps if you posted your Constructor so we don’t have to guess?
The superclass will not alter attributes unique to the subclass, and should be the FIRST line in the subclass constructor

if you want to alter the # of columns based on the # of fields in a database… then do this when you have connected to the required table

I’ll post a video in a while. I need a few minutes :slight_smile:

good luck… I doubt a video will provide any more insight… hope you get it figured out

Here we go:
https://youtu.be/qTpMyuC2ALw

The Constructor is shown
BTW: Does anyone know how to embed a YouTube video into the forum?
Thank you in advance

Are you sure your Constructor Code is executed after the

Super.Constructor

line?

Yes. I’ve setup a breakpoint and confirmed that

Modify your code to implement the Open Event and move the code you added to the constructor there. I believe you can’t set number of columns in the constructor. So add a private variable you set in the constructor and then read that in the open event to actually make the change.

Thank you very much. Now it works :slight_smile:
Although I am not an expert, this XOJO’s approach is bit out of OOP principles

It’s only like this for controls. Every other object follows standard OOP procedures but controls are a little different because they don’t actually exist until the open event has fired. This caught me a couple times when I was first starting with Xojo several years back.

Not really. What happens is that first the constructor runs – there must be an object before it can be modified of course –, and then the Inspector properties are transferred to the object, like ColumnCount, which can override what you defined in the constructor.

If you want ColumCount to be different than the standard (which is 1 for a listbox) in your subclass without setting the value in Inspector, you can change its default value in the subclass’ Inspector behavior properties by right-clicking on the class. Change ColumnCount to 3 and this will be your new standard.

EDIT: You can also change the value and deselect the property in Inspector behavior so this property will not appear in Inspector anymore.

I tried it, but without success

BTW: How to make visible again a Declaration in the Inspector Behavior?

Oh, you’re right, ignore the last sentence. Don’t disable the inspector property. Somehow it seemed to work, but only on the first attempt.