Subclasses are your friends

There are a LOT of things that you will hav a hard time doing if you only ever add instances of controls from the Library in Xojo to your layouts. And you will spend a LOT of time trying to sort out how to beat them into submission to do what you want with some combination of handling the events, properties and methods on your layout.

Like this fun little request that came in on an off forum chat service.

When you do “CMD-A” in a listbox it selects ALL rows…. but is there a way to select all “but certain” rows… rows that should never be selected?

With just an instance on a layout and the handful of events you have available you could probably make this work – eventually.

But I made a subclass and within 5 minutes (or less) had this

And very quickly altered it to this

The reason I could do this so quickly ? A subclass.

In a subclass you can add methods, properties, events, new events using event definitions and you CAN also add things like Menu Handlers. And therein lies the crucial piece of information.

In an instance placed directly on a window you have no opportunity to add menu handlers so you have to devise some other means to catch the “Select All” keystroke or menu selection and try to do something else.

But in the subclass I CAN add whatever menu handlers I want – I can even add menu handlers for menu items that do not exist yet (yes really !). And this makes is possible for us to add a menu handler for EditSelectAll – the default name for that menu item in a desktop project. If you happen to rename this menu then you would need to update the menu handler implementation to match.

As soon as you do that and place an instance of your custom subclass on a layout you will now be able to handle the Select All menu however you want.

And that makes it possible for you to use all kinds of things that are not normally accessible when you put an instance directly on a layout and try to make it behave specially.

Subclassing is a VERY powerful way to make your customized versions of the normal controls and classes Xojo provides as part of its framework.