Detect a textchange for all textfields in a window.

  1. 4 months ago

    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?

    @FrancescoMarciano 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.

    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. :)

  2. Paul S

    Mar 21 Pre-Release Testers, Xojo Pro Europe (Netherlands, Den Haag)

    Have a look at the ContentsChanged property.
    http://docs.xojo.com/Window.ContentsChanged_property

  3. Edited 4 months ago

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

  4. Sascha S

    Mar 21 Pre-Release Testers, Xojo Pro Answer Germany

    @FrancescoMarciano 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.

    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. :)

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

  6. Sascha S

    Mar 21 Pre-Release Testers, Xojo Pro Germany

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

    Sure :)

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

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

  8. 3 months ago

    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?

  9. Bob K

    Apr 10 Pre-Release Testers, Xojo Pro Kansas City

    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://docs.xojo.com/RectControl.Parent
    https://docs.xojo.com/RectControl.TrueWindow

  10. 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!"
  11. This don't works:

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

    Apr 11 Pre-Release Testers
    Edited 3 months ago

    @FrancescoMarciano This don't works:
    Dim mywin As Window = Me.TrueWindow

    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/

  13. 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".

  14. Michel B

    Apr 12 Pre-Release Testers RubberViews.com

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

    http://docs.xojo.com/Introspection

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

    Dim t As Introspection.TypeInfo
    t = Introspection.GetType(me.TrueWindow)
    
    MsgBox  "My class name is " + t.Name
  15. Ivan T

    Apr 12 Pre-Release Testers
    Edited 3 months ago

    @FrancescoMarciano 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".

    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

  16. Thanks to all, men! Bye!

or Sign Up to reply!