How to create Custom Event in Subclass

I’ve subclassed ‘WebSearchField’, and added Event Definitions and Event Handlers for ‘Opening’ and ‘Shown’ and other built-in Events.

Now I want to create a Method in the subclass and ‘Raise’ it to the Instance.

E.g., I create a method called ‘ProcessFoundRecord’ in the subclass, but would like to implement this Method in the Instance instead.

What is the technique for implementing an Instance Method that is defined in the subclass?

If I understand you correctly, you have a method ProcessFoundRecord in a class that inherits from a Web UI control; and you would like to override that method on a particular instance of the subclass?

You can’t do that. Individual instances of classes can’t have their own code that overrides the class functionality - they can only implement events and manipulate properties.

If you want to override the class’s method, you’ll have to subclass it and do the overriding in the subclass.

You drag your subclass on the window instead of a generic WebSearchField. It will have the method built in. I don’t understand the question.

However, your title mentions an Event, so perhaps you are looking for RaiseEvent?

Please explain further.

Hi @Tim_Hare , yes - I’m wanting to implement (most) of the Method behavior in the class, and then Raise so instances of this class can create their own added behavior.

Within your ProcessFoundRecord method of your class, you’ll do most (or all) of the work, then call the Event Definition you’ve added to the class (potentially with parameters and a return value).

Event RecordFound(record as DatabaseRow)

Sub ProcessFoundRecord(record as DatabaseRow)
  '// Do something with record here
  RaiseEvent RecordFound(record)
End Sub

If you’re trying to override the functionality, you could create your event definition with a boolean return value:

Event RecordFound(record as DatabaseRow) as Boolean

Sub ProcessFoundRecord(record as DatabaseRow)
  '// Return True from RecordFound to cancel the default operation
  If RecordFound(record) = True Then Return

  '// Do something with record here
End Sub

Here’s an example project showing both to get you started.
customsearchfieldevents.zip (8.1 KB)

in xojo you can code in the class template event or in the Instance event which is in a window used but not both.
a class method can be called from both.

means xojo call only one method for the defined event in the template or in the Instances.
from my point of view it should call both places.

If you’re wanting to re-raise the same event to the instances, all you need to do is right-click on the implemented event in the subclass and select “Create Event Definition”. The IDE will clone the event to an identical event definition.

1 Like

Thanks to everyone! @Anthony_G_Cyphers this was exactly what I was shooting for - problem solved!

1 Like