Detect a textchange for all textfields in a window.

Hi all!
I have a window with a lot of textfields, textareas, checkbutton, etc. and i need to detect when i have a change of text/value in these fields in real time (to propose a “Save changes?” when the user close the window).
I know that i can write code in the textchange event, i.e. for a textfield, to detect the change but i’m looking for a method or an extension for all my textfields in the window. I have no arrays of textfields but single fields.
There’s a simple strategy to obtain the purpose?

Have a look at the ContentsChanged property.
http://documentation.xojo.com/api/deprecated/window.html#window-contentschanged_property

Thanks, Paul, i will try!
Which controls are affected by this property?

A well-intentioned tip for the future: Always create your own Controls from Subclasses of the available Classes. So you can next time just add your code which is needed in all controls of specific classes, to your own Subclass. :slight_smile:

A great advice, Sasha!!! I can still make this change to my code, right?

Sure :slight_smile:

Just create a TextField Subclass and then change the Super of your Controls in the Container/Window.

This solve my problem!
Thanks to all and ciao Sascha!!!

Excuse me if I wake up this post!
The Sasha’s solution works fine but if i want to use the same subclass in different windows how i can pass the window’s reference?

You can find out the parent of the control by using ControName.Parent. This could be a container control. If you want the ultimate parent window you can use ControName.TrueWindow.

https://documentation.xojo.com/api/deprecated/rectcontrol.html#rectcontrol-parent
https://documentation.xojo.com/api/deprecated/rectcontrol.html#rectcontrol-truewindow

I’m not able to perform this task!
In an event (like TextChange) of the class i want obtain for example:

Dim mywin As Window mywin = (the parent window of my controls) mywin.label1.Text = "Modified!"

all woks if i insert my window in the code, obviously:

Window1.label1.Text = "Modified!"

This don’t works:

Dim mywin As Window = Me.TrueWindow mywin.label1.text = "Modified!"

[quote=432408:@Francesco Marciano]This don’t works:
Dim mywin As Window = Me.TrueWindow
[/quote]

You have problems with the OOP. YourWindow is a subclass of Window with extra properties (controls)

If you declare a variable as the super class Window, it will not have access to the subclased items of YourWindow

You have to declare a variable of the type YourWindow and when asigning the Me.TrueWindow, you have to cast the objet to YourWindow type

Dim mywin As Window1 = Window1(Me.TrueWindow)
mywin.Label1.Text = "xxxxxx"

BUT

Maybe this is more suitable to your problem:
https://blog.xojo.com/2016/06/15/design-patterns-in-xojo-observer-part-1/

Ivan, your code is the same as this:

Window1.Label1.Text = "xxxxxx"

A simple summary:

  • i have two windows (Window1 and Window2) with a textfield and a label each one;
  • i created a subclass of the textfield class with an event handler for the textchange event;
  • the super of my textfields in the windows is my subclass;
  • i want to share this subclass with my two windows;
  • i need a reference of the source window in the textchange event to write a code like this: mywin.label1.text = “modified” where mywin could be Window1 or Window2;

It seems a simple question (for me) but maybe i really have “problems with the OOP”.

From the subclass, I believe you can get the name of the window by using Introspection.

http://documentation.xojo.com/api/language/introspection.html

Slightly modified, the first example sould give you the information you need :

[code]Dim t As Introspection.TypeInfo
t = Introspection.GetType(me.TrueWindow)

MsgBox "My class name is " + t.Name[/code]

[quote=432489:@Francesco Marciano]Ivan, your code is the same as this:

Window1.Label1.Text = "xxxxxx"

A simple summary:

  • i have two windows (Window1 and Window2) with a textfield and a label each one;
  • i created a subclass of the textfield class with an event handler for the textchange event;
  • the super of my textfields in the windows is my subclass;
  • i want to share this subclass with my two windows;
  • i need a reference of the source window in the textchange event to write a code like this: mywin.label1.text = “modified” where mywin could be Window1 or Window2;

It seems a simple question (for me) but maybe i really have “problems with the OOP”.[/quote]

Yes I think you do. My code it is not the same. maybe it is not clear what I meant with “super”, Window1 and Window2 are two objects with diferent types. Those 2 are subclases, their super is Window. A reference of type Window, can’t access the propertys of the subclases.

If you have only 2 windows with implicit instance, just use a static reference using a if to send the message to Window1 or Window2.

If you have more windows, or more than one instance of each, better use something like the Observer Design Pattern

Thanks to all, men! Bye!