Subclassing Text Field

Hello all,

I am trying to make heads or tails of the OO features of Xojo but I’m at my wit’s end even after reading the relevant docs. Maybe I’m just dense.

Here’s what I’m trying to do:

I would like to subclass the stock TextField (let’s call it SmarterTextField), save the text value on LostFocus in savedValue if it’s changed from its previous value (if self.savedValue <> self.Text) and raise a new event, let’s call it ValueChanged.

The goal is pretty obvious - the current impl of TextField calls TextChange on every keystroke. I just want it to happen when the user is done editing the text and the final value has changed from it’s original text when the field received focus. Obviously I want to re-use this class in many places.

What I tried:
I sub-classed TextField, added a public string property called savedValue. Yet my instances of SmarterTextField do not see this new property. I also experimented with Shared Properties but further reading taught me that’s not at all what I’m looking for… I tried a few other things with defining events and whatnot, but that got me no further.

Help? TIA!

Are you calling SavedValue as me.SavedValue or as SmarterTextField.SavedValue ? The first one will work, not the second. The property on the master class is not available. You can only access the property on the instances, for instance SmarterTextField1.SavedValue.

When you say that your instances “do not see” the property, can you clarify that?

  • is it that autocomplete doesn’t work?
  • is it that it doesn’t appear in the inspector?
  • is it that you get a compile error?

For what it’s worth, if you are just changing the Super of an existing instance, you may need to close the Window and reopen it for the changes to take effect there.

Thank you both. I can now access my subclass property (savedValue) from both the window instance and the subclass itself by doing me.savedValue. Not sure if I hadn’t tried me.savedValue (pretty sure I did) or maybe like Greg said, reopening the project did the trick (I think it’s the later but who knows.)

Next problem: I’d like to deal with the GotFocus event in both the subclass and the instance. However, when I add some code in GotFocus of the subclass, I get a compile error that the instance implements GotFocus but its superclass SmartField has already implemented the event.

(Next problem++: how can I generate a new event from the subclass (ie: mynewevent) so that I can implement instance specific code ?)

Thanks again for your help!

Ok, I figured out how to raise a new event from my class, so that’s fine…

I can see how how I could raise an event from the subclass GotFocus method to generate MyNewGotFocus, but that seems awkward and unnecessary. Isn’t there a way to have a handler at each level of the object hierarchy ?

In your subclass you can create an event definition for GotFocus which you raise in your event handler for GotFocus from the super class. It is perfectly acceptable to define the event with the same name as an event you are implementing.

[quote=145298:@Philippe Tanguay]
I just want it to happen when the user is done editing the text [/quote]
I’m curious how you plan to determine this since you cannot rely on lost focus to tell you “I’m done”
A keypress would work (tab return etc)
But clicking a button does not always move the focus to the button so your subclass would not necessarily receive a lost focus event (this is more platform dependent than anything)

Norman, could you not add a me.setfocus event to a subclassed button.action event? Do you know offhand which controls may not fire a getfocus event like you describe?

You’d have to do “something” to make it so the text field could plausibly know when the user has done something else like pressing a button etc that doesn’t trigger a focus change.

We’ve had much the same issue in the IDE where you might rename a method then hit “Run” expecting the new name of the method to be used and not the old one.
That was in fact one of the very first decent sized bugs I fixed in the old IDE way back when I first joined.

The trick is that you assume that lost focus is adequate BUT then you have cases where lost focus won’t get raised so you have to do something else.