Dealing with events in an instance of a custom canvas subclass

This may be something of a noob question, but I’ve never really needed to figure this out, and so far my reading both on this forum and in the manual is not giving the direction I need.

I have created a subclass of canvas we’ll call CustomCanvas. In general, there are some things that I want to have happen in, say, the MouseEnter event of ALL CustomCanvas instances. So I implement the MouseEnter event in CustomCanvas.

Then I add an instance of CustomCanvas to a window, and discover that for this specific instance of CustomCanvas, I need it to do a few more things when MouseEnter fires. But I don’t want all CustomCanvases to behave this way, just this specific instance.

However, I cannot add an event handler to this instance, because it is already in the class.

So, to sum up: How can I add some custom behavior to an instance of a canvas subclass that augments an event that is implemented in the subclass?

I’d prefer to do this without creating yet another subclass of my subclass.

I suspect there will be some event definitions in here somewhere, but I’m fuzzy on exactly how those work and if they would apply here.

I got around this by making the things I want them all to do in the MouseEnter event of the master, and then at the end of the MouseEnter event on the master I perform essentially NextSteps. Define NextSteps as an Event Definition and then on any of the CustomCanvases you need to do extra things make a Event Handler for NextSteps.

Did that make sense? It makes sense in my head (and works in my project) :slight_smile:

Here’s a project to show you what I’m talking about nextSteps.zip (5kb - Dropbox)

[quote=84843:@Kimball Larsen]This may be something of a noob question, but I’ve never really needed to figure this out, and so far my reading both on this forum and in the manual is not giving the direction I need.

I have created a subclass of canvas we’ll call CustomCanvas. In general, there are some things that I want to have happen in, say, the MouseEnter event of ALL CustomCanvas instances. So I implement the MouseEnter event in CustomCanvas.

Then I add an instance of CustomCanvas to a window, and discover that for this specific instance of CustomCanvas, I need it to do a few more things when MouseEnter fires. But I don’t want all CustomCanvases to behave this way, just this specific instance.

However, I cannot add an event handler to this instance, because it is already in the class.

So, to sum up: How can I add some custom behavior to an instance of a canvas subclass that augments an event that is implemented in the subclass?

I’d prefer to do this without creating yet another subclass of my subclass.

I suspect there will be some event definitions in here somewhere, but I’m fuzzy on exactly how those work and if they would apply here.[/quote]

You add an event definition to your custom subclass and raise the event in your implemented MouseEnter event in your custom subclass

So you have something like

[code] Class MyCustomSubclass
inherits Canvas
Event MouseEnter() // this is the new event that instances will respond to when they implement it

 Sub MouseEnter() Handles Event MouseEnter 
         // this is the code you have in your current event handler in your custom subclass
         // … and so on …
         raiseEvent MouseEnter()
    end sub

end class
[/code]
then you put an instance of this on your layout & can implement the mouse evener event (or not)

Norman called his new event Mouse_Enter, but I’ve found you can also call it MouseEnter and it works fine, so it looks like a normal canvas would when you go to add the event handler.

[quote=84843:@Kimball Larsen]So, to sum up: How can I add some custom behavior to an instance of a canvas subclass that augments an event that is implemented in the subclass?

I’d prefer to do this without creating yet another subclass of my subclass.[/quote]

If your parent class is CustomCanvas, you may have several subclasses, such as CustomCanvas1 and CustomCanvas2.

Use conditional execution in your event such as :

If Me.Name = "CustomCanvas1" then //Handle CustomCanvas1 stuff elseif Me.Name = "CustomCanvas2" then //handle CustomCanvas2 stuff end if

No need to raise extra events.

I routinely do things the way Norm described BUT I name the new event the old event. I find it the most flexible and maintainable way and if you distribute using the same name has the advantage of looking and behaving like a standard Xojo control in terms of using events… getting back to the principle of least astonishment

That seems like it could get very hard to maintain when you’ve got a large number of controls though.

Actually it’s named “MouseEnter”
see [quote] Event MouseEnter() // this is the new event that instances will respond to when they implement it[/quote]
and the typo was in the RAISE which would have complained when you compiled it saying there is no such event :slight_smile:

Wow - ask a question, run out to lunch and come back to find answers for dessert. Nice!

The solution definitely appears to be to define a custom event definition in the subclass, and then add that event handler in the specific instance. When I tried this before asking this question, the IDE did NOT show my new custom event definition as one of the events I could add a handler for. Turns out I have to either close / reopen the project or at least flip around various tabs to get the new event definition to show up in the list of implementable event handlers when adding a new event handler.

I wonder if this is because the canvas to which I wanted to add the new event handler is a member of a control array. I’ll see if I can reproduce this as a specific bug in an example program, and file a bug report.

Actually, this looks like a known bug.

If you click on a window control…venture within a code area…then attempt to access the custom event definition it will suddenly appear…no need to close and reopen the project :slight_smile: