Sharing a method between container controls

I have two container controls I use as buttons. Each contains just a canvas. As the buttons look and behave similarly, the Paint event for each canvas has the same code. I’d like to put the code in a method, in particular since I will be making one of the buttons draggable, and will want to call this method to create the button outline during the drag.

Where is the best place to store such a method? The easy way I suppose would be a folder labelled “Button Bits” or similar but I wonder if there is more appropriate approach.

Create a new class and set it’s super to ContainerControl. Put the common methods in there, then set the super of each of your controls to this new class.

The same technique works for Windows.

3 Likes

Good. Thanks.

Kem, I do not understand exactly what that means, but the way I’m doing it is: create a class with the common method and use that class as a property in any ContainerControl, OR does that mean the same ?

Not the same.

Kem creates a common superclass to hold common code (so the code is part of that superclass and all it’s subclasses)

You create a separate class to hold the method and add it as a property. You can do that to any other class too.

I can confirm that if you do precisely what @Kem_Tekinay outlined, then it works as desired. I got it wrong when I first tried his suggestion by not reading it properly, and instead creating a new ContainerControl and making that the super, which the compiler didn’t like.

The thing to remember with Window and ContainerControl is you can’t subclass them once you have added layout. You can subclass a “pure” window/container - one that has only code - but once you put controls on it, it’s done.

Odd.

Odd because I’m sure I hadn’t done that to the new CC before changing my CCs’ supers to be the new CC. I did, however, change the height/width of the new CC to be other than the default it comes with. Would that count as changing it, within the meaning of the Act?