Send messages from subform to parent form

azwierzchowskiazwierzchowski Member Posts: 15
edited 2012-05-11 in NAV Tips & Tricks
You can send message from subform to parent form using XMLDocument and events. When you call XMLDoc.LoadXML it fires OnreadyStateChanged event, so:
1. Declare "'Microsoft XML, v3.0'.DOMDocument" automation variable with events in parent form, and without events in subform
2. Create variable in OnOpen trigger in parent form, and pass it to subform
3. Handle OnReadyStateChanged event in parent form:
IF (XMLDOC.readyState = 4) THEN
ProcessXMLMessages();
and create procedure ProcessXMLMessages with your code
4. call XMLDoc.loadXML in subform every time, you want to notify parent form

Comments

  • Joe_MathisJoe_Mathis Member Posts: 173
    I know that this is an old post, but as it is directly showing what I want to do...

    How are you passing the XMLDoc to your subform in step 2?

    I am trying to use Sales Header and Sales line and keep a running total in the Header witout needing to select it everytime.

    Could you please post a code example?

    Thanks,

    Joe
  • azwierzchowskiazwierzchowski Member Posts: 15
    Here it is:

    OBJECT Form 50000 Parent
    {
    OBJECT-PROPERTIES
    {
    Date=07-08-30;
    Time=15:14:11;
    Modified=Yes;
    Version List=;
    }
    PROPERTIES
    {
    Width=8000;
    Height=4840;
    OnOpenForm=BEGIN
    CREATE(XmlDoc);
    CurrForm.SubForm.FORM.SetXmlDoc(XmlDoc);
    END;

    OnCloseForm=BEGIN
    CLEAR(XmlDoc);
    END;

    }
    CONTROLS
    {
    { 1000000000;TextBox;330 ;220 ;4290 ;440 ;Editable=No;
    SourceExpr=TextVar }
    { 1000000001;SubForm;220 ;880 ;7480 ;2310 ;Name=SubForm;
    SubFormID=Form50001 }
    }
    CODE
    {
    VAR
    TextVar@1000000000 : Text[50];
    XmlDoc@1000000001 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 4.0:{88D969C0-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v4.0'.DOMDocument40" WITHEVENTS;

    EVENT XmlDoc@1000000001::ondataavailable@198();
    BEGIN
    END;

    EVENT XmlDoc@1000000001::onreadystatechange@-609();
    BEGIN
    IF (XmlDoc.readyState = 4) THEN BEGIN
    TextVar := CurrForm.SubForm.FORM.GetText();
    CurrForm.UPDATE(FALSE);
    END;
    END;

    BEGIN
    END.
    }
    }

    OBJECT Form 50001 Child
    {
    OBJECT-PROPERTIES
    {
    Date=07-08-30;
    Time=15:19:54;
    Modified=Yes;
    Version List=;
    }
    PROPERTIES
    {
    Width=8030;
    Height=2420;
    }
    CONTROLS
    {
    { 1000000000;TextBox;220 ;220 ;4180 ;440 ;SourceExpr=TextVar1;
    OnAfterValidate=BEGIN
    SendMessage();
    END;
    }
    { 1000000001;TextBox;220 ;770 ;4180 ;440 ;SourceExpr=TextVar2 }
    }
    CODE
    {
    VAR
    TextVar1@1000000000 : Text[50];
    TextVar2@1000000002 : Text[50];
    XmlDoc@1000000001 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 4.0:{88D969C0-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v4.0'.DOMDocument40";

    PROCEDURE SetXmlDoc@1000000000(pXmlDoc@1000000000 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 4.0:{88D969C0-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v4.0'.DOMDocument40");
    BEGIN
    XmlDoc := pXmlDoc;
    END;

    PROCEDURE SendMessage@1000000004();
    BEGIN
    XmlDoc.loadXML('<root></root>');
    END;

    PROCEDURE GetText@1000000001() : Text[50];
    BEGIN
    EXIT(TextVar1);
    END;

    BEGIN
    END.
    }
    }
  • Joe_MathisJoe_Mathis Member Posts: 173
    Thank you so very much. :D

    This enabled me to do EXACTLY what I wanted.

    =D>
  • ajhvdbajhvdb Member Posts: 672
    This is a replacement for the timer solution and the singleinstance codeunit to pass values from form to form?

    Looks a bit technical...Does it work in a live situation?
  • Joe_MathisJoe_Mathis Member Posts: 173
    Yes, this replaces timer and singleinstance codeunit solutions.

    It may seem complex at first, but it's only 6 lines of code in the Parent form and 4 lines of code (3 functions ) in the subform.

    I didn't need it to pass back information like his example, so I got it down to 5 lines in the parent and 3 in the subform.

    Then I decided I wanted two more fields to update the main form, so I added the "SendMessage();" to the "OnDeactivate" trigger of those fields of the subform and it worked great.

    Yes it works live.

    Definately a great tip (or trick).
    Once again, Thank you azwierzchowski =D>
  • Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    Somehow I'm not convinced my user will have XMLDOM on the Win98 boxes they use for tills, so I think I'll just go with the timer trick. It all seems fine here but it's a powerful PC with a single-used DB. Has anybody found any performance problems with it in real life?
  • Miklos_HollenderMiklos_Hollender Member Posts: 1,598
    OK I've did a bit of benchmarking on the timer-based solution, I've put it behind a button:

    [code]
    MESSAGE(FORMAT(TIME));
    FOR i:=1 TO 100000 DO BEGIN
    IF CurrForm.PaymentLines.FORM.NeedsUpdating THEN BEGIN
    CurrForm.UPDATE(TRUE);
    END;
    END;
    MESSAGE(FORMAT(TIME));
    [code]

    This takes 1 sec in my fast PC. Let's assume ten times slower PC's it means checking the subform and finding out that no, we don't need to update it right now takes 1/10000 of a second. Well that performance hit we can live with :D

    The advantage is that I'm not sure they have XMLDOM in Win98. Though I think it's built into IE which is built into Windows but why risk it.
  • st0328st0328 Member Posts: 97
    You can send message from subform to parent form using XMLDocument and events. When you call XMLDoc.LoadXML it fires OnreadyStateChanged event, so:
    1. Declare "'Microsoft XML, v3.0'.DOMDocument" automation variable with events in parent form, and without events in subform
    2. Create variable in OnOpen trigger in parent form, and pass it to subform
    3. Handle OnReadyStateChanged event in parent form:
    IF (XMLDOC.readyState = 4) THEN
    ProcessXMLMessages();
    and create procedure ProcessXMLMessages with your code
    4. call XMLDoc.loadXML in subform every time, you want to notify parent form


    Hi azwierzchowski ,
    Till point 2 i can able to follow. Please explain Point 3 & 4
  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    st0328 wrote:
    Hi azwierzchowski ,
    Till point 2 i can able to follow. Please explain Point 3 & 4
    Did you read the whole thread? He has posted example code.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
Sign In or Register to comment.