Forum Discussion

Andrey_M's avatar
Andrey_M
Contributor
13 years ago

User forms: how to get form reference from handler?


Hi guys.


 


Finally I started playing with user forms and found that I can not use relative parent form reference from handler procedure.


 


Why I need this? Sometimes the behaviour of form objects should depend on status of another object(s). To refer such dependent objects I have to refer to parent form using either direct form reference (UserForm.<formName>.objectName) or using global variable which contains reference to the form (<myForm>.objectName). Both methods are not good because they use hardcoded names.


 


Example:


 


I created form which contains memo field and two buttons Ok and Exit. I want to have Ok button disabled if memo field is empty.


 


Following piece of code:


 


function main()


{


  myForm = UserForms.myDemo; // global variable


  myForm.ShowModal();


}


 


 


// memo field handler: OnChange event


function myDemo_myText_OnChange( Sender )


{


  UserForms.myDemo.btnOk.Enabled = /[^\n\r ]/.test( Sender.Lines.Text ); // enable Ok button if memo field contains readable symbol(s)


}


 


// OnChange handler can be rewritten using global form variable:


 


function myDemo_myText_OnChange( Sender )


{


  myForm.btnOk.Enabled = /[^\n\r ]/.test( Sender.Lines.Text );


}


 


I want something like this (relative reference to parent form):


 


function myDemo_myText_OnChange( Sender )


{


  Sender.parent.btnOk.Enabled = /[^\n\r ]/.test( Sender.Lines.Text );


}


 


Can I get similar functionality without having direct reference to parent form?




Regards,

Andrey

2 Replies

  • hlalumiere's avatar
    hlalumiere
    Regular Contributor
    What exactly isn't working in this case? Do you get an error? Or is the .Parent property just absent?
  • Yes, there is no such property as Sender.Parent (this is why I am asking about workaround).



    In case if handler does not know the name of form or name of parent object it is not possible to address another [logically relatedt] object from handler of specific one.



    If I decide to use the same combination (subset) of objects on different form I have to follow strict rules on naming.