Custom Class/Control with containing a Canvas

I have created a subclassed ListBox that has the events and properties I want.
It also uses a custom canvas which I’d like to have as part of the custom ListBox class, but I can’t seem to cut/paste the canvas from the Main Window to have it as “part” of the Custom ListBox. It accesses the custom Listbox’s properties so I’d like it to be part of the subclass.
How would I do this?
Hopefully I’m making sense.

classes cannot contain other classes

however there are alternatives I’ve discussed on my blog

Thank you. Makes for some messy code, but so it goes!

put the listbox & canvas in a module
make the listbox global
make the canvas private
then the listbox can create instances of that canvas but nothing outside the module can

that should do it

its about as close as you get

[quote=453104:@Andy Broughton]I have created a subclassed ListBox that has the events and properties I want.
It also uses a custom canvas which I’d like to have as part of the custom ListBox class, but I can’t seem to cut/paste the canvas from the Main Window to have it as “part” of the Custom ListBox. It accesses the custom Listbox’s properties so I’d like it to be part of the subclass.
How would I do this?
Hopefully I’m making sense.[/quote]

Why dont you use a container and put both, the listbox and the canvas on it. It is a lot less messy code

just then to make the container act like a listbox you have to expose a ton of the methods on the container, plus the events etc

been there done that and it can work but its a lot of work

Naaaaa, no need of manually expose methods and properties…

Container.Listbox.Whatever

Actually quite easy, just right click on any event of the listbox, click on Create Event Definition from Event. Done!, 2 clicks

If he es not making a comercial product, there is no case to “over-engineer” the code

[quote=453123:@Ivan Tellez]Naaaaa, no need of manually expose methods and properties…

Container.Listbox.Whatever

[/quote]

Whether or not this is for personal or commercial use I would never recommend this style
Encapsulate things properly and then you can, if you want, change the internal implementation without affecting anything that uses this. Not doing so makes that job much harder if you ever want to do it.

And not doing it can make for some nasty unmaintainable code regardless of what you plan to do long term

A custom control should appear to your app as any other control…with a specific set of events, properties and methods
Any embedded control should be private and invisible to the outside world.

Shure, rules are there for something :wink:

https://www.youtube.com/watch?v=yijQRhPvJhQ

Not a rule - just a “good habit” to get into
It makes no difference to me if you write your own software that way
I try not to so the components I make are something I ca neasily lift into any other app
Heck I’ve written listbox replacements that have much nicer behaviour for headers etc
And, because my container control appears to be a listbox in every respect, it has the exact same API plus extra methods, events and properties, it can be easily substituted for the built in listbox

In the particular case of wanting a canvas in a listbox, wouldn’t it be an option to use the cellbackgroundpaint event in the listbox as canvas?

You wouldn’t be able to use your custom canvas, of course.

Julen