Subclassing the Listbox - best and correct method?

Hello all,

I have just come back to Xojo after about 2 years off. My memory is a bit rusty and am having some trouble with creating a subclass of the Listbox for the purpose of creating grids which will replace the Einhugur stylegrid.

I have been able to create a new class, with the master class of Listbox. But what I am wanting to do is to take that further and have a somewhat generic new class that includes some methods and, be able to add methods to the instance of the new class. In addition, a set of events with code already in place that is duplicated AND editable for each specific instance of the new class is highly desired.

So far, I can create a new class with a Super of Listbox. I can add properties, events and methods. But when a new instance is created, each property has to be created, code placed (copied) and I cannot create/add methods to the new instance.

What is the correct way to get what I am looking for?
Your help is appreciated!
Tim

How do you place the new class on the Window? Have you tried to change the super to your listbox subclass?

Hello Beatrix,

Yes, on the form it has a super of the subclass. But I cannot add/change any methods. Further, there is not “list” of methods, although they are available via code due to their scope.
Tim

That’s not the purpose of subclassing. You are supposed to override the methods in the instance. There never has been a list of methods.

OK, no list. But I also cannot add any new methods to the instance.
Tim

You add another class to your project and make its Super your listbox subclass. Add methods to that, in addition to the methods of your subclass to extend your subclass and add additional functionality. Create an instance of that class on the window. You can continue to create new classes and add specific functionality to each to achieve the breadth and/or depth of subclassing you desire.

You add methods to a subclass of the subclass, not an instance.

You cannot add anything to an instance of a class, you have to add properties, methods and events to a class (in that case your listbox subclass). All instances of that class will then have these properties, methods and events available.

Hi Tim and Eli,

Eli - that is exactly what I do not want. Or to be more specific, I need each instance to be somewhat unique.

To be clearer - I want a generic instance, but the ability to edit the existing code, and create/add additional methods and properties to each instance.

Just thinking about that - can I add a new class to an instance of the subclass?
Tim

You need to think about what is the same and what is changing in your listbox. Can you tell us a little more about what you want to do?

How familiar are you with patterns? Sounds like you need something like the strategy one. There are examples in the Xojo Examples folder.

Or you need to make one main subclass of the listbox for your common behavior and then several subclasses of this main class.

Hello Beatrix.

I have not ever heard of “patterns”. Will have a look.
Thank you,

No, you can only subclass classes.

I think you get confused by the fact that you can add methods to Windows and ContainerControls.

Those are somewhat special, and not the same as the control instances they contain.

To understand how subclassing etc works look at this:

You have a class ANIMAL

ANIMAL has subclasses MAMMAL, BIRD, FISH

MAMMAL has subclasses DOGS, CATS, HORSES

DOGS has subclasses POODLE, TERRIER, COLLIE, MONGREL

Each subclass will have more specific properties, events, and methods that distinguish it from the other subclasses (eg what distinguishes a dog from a cat? What a poodle from a collie or terrier?)

You can have an INSTANCE of COLLIE which is a dog called Lassie.

But how can you have a “subclass” of Lassie? Makes no sense.

How could Lassie have methods (like WagTail) that are not already defined in the class hierarchy since Lassie is an instance of the class?

Whatever Lassie has is defined in the class - because the class is the blueprint on which Lassie is made.

Btw: if you give your subclass an event definition then each instance will specify what happens when that event is raised

Lets say in the subclass DOG you add an event definition EMERGENCY_RESPONSE.

In COLLIE you put “run and get help” into the event

In POODLE you put “YAP YAP YAP” into the event.

In TERRIER you put “attack whatever is there”

Method code is shared by all instances (unless you override it).

Event code is specific to each instance (that’s why all your PushButtons etc react differently - because you put different code into their Action event)

Thanks for the simple explanation Markus.

I guess what I was trying/wanting to do was to encapsulate all relative code to a specific instance of a class. So when looking for the method for a listbox on a specific form, the method would be part of it, not elsewhere. In this case too, I do not think that methods are reusable since each listbox does something very specific depending on the form etc.

Part of that also comes from what I find as difficulty with this new, or not so new, Xojo IDE. I still like the layout of the old one. With Windows, the Inspector has to be dragged out a lot - looks like the objects are right justified instead of left, and the Navigator tends to jump to unwanted areas from time to time, and scroll with difficulty. I have heard it is better when using a Mac, but I do not use one.

Anyway that was part of the reason.
Thanks for all of the answers and feedback - this forum is fantastic!
Tim

[quote=225581:@Tim Seyfarth]Part of that also comes from what I find as difficulty with this new, or not so new, Xojo IDE. I still like the layout of the old one. With Windows, the Inspector has to be dragged out a lot - looks like the objects are right justified instead of left, and the Navigator tends to jump to unwanted areas from time to time, and scroll with difficulty. I have heard it is better when using a Mac, but I do not use one.
[/quote]

If you do not need specific features of the most recent Xojo, such as new framework, nothing prevents you from creating the program in RS, and use Xojo only to build. There are several members here who just do that.

I happen to like the Xojo IDE, but what works for you works for you.

Yes, I know. I had been doing that, trying the new IDE from time to time, but now it is time to move forward. There have been SO Many advances made “under the hood” that it is crazy not to use them! What’s more, I’ve been keeping my subscription active eventhough I have not been active.

Bottom line - I just have got to “get over it”!

Tim

[quote=225597:@Tim Seyfarth]Yes, I know. I had been doing that, trying the new IDE from time to time, but now it is time to move forward. There have been SO Many advances made “under the hood” that it is crazy not to use them! What’s more, I’ve been keeping my subscription active eventhough I have not been active.

Bottom line - I just have got to “get over it”!
[/quote]

Welcome to the 21st Century :wink:

I use name-prefixing to group things in Xojo. So if your listbox is named weatherLB you can add the methods to your window named like weatherLBShowNight or weatherShowNight or weather_ShowNight. Then when you type MyWindow.weather… it’ll autocomplete to show your listbox and any associated methods.

I do this any time an object has too many parts and I feel a hesitation when finding something. In this example my window naturally has groups for forecast, history, plot, subview, and plot has sub-groups plotCollect and plotSec. There’s more parts in my window and not all are prefix-named but it really helps to not lose things in the pile. ymmv

Controls
forecastLB
forecastLoadButton
historyLB
historyLoadButton
plotCollectButton
plotCollectInfoLabel
plotDisplay
plotFromField
plotMaxField
plotMinField
plotUpdateButton
subviewPagePanel
subviewSegControl

Methods
forecastFindTempRange
historyFindTempRange
plotCalculateBounds

Properties
plotSecMax
plotSecMin

Interesting Will.

I prefix common - like grdAbc

You use similar but make the object type at the end… interesting POV. I will try that and see how it sorts. It is completely backward from what I am used to, but everything is changing so maybe that is a good time to include this too!

Thanks for your feedback and suggestion Will,
Tim

Events might be very useful to you. They allow you to put code in the instance. Think of them as “hooks” that you call from your main class wherever you need instance-specific functionality.