Tuesday, November 18, 2014

Encapsulating Popup Calls Within Forms


One of my dissatisfaction when creating a popup shortcut via Define Popup and later On Selection Popup or On Selection Bar commands is its insistence of not accepting Thisform reference.  Something like this:

Define Popup PopFaves From Mrow(),Mcol() Margin SHORTCUT
Define Bar 1 Of PopFaves Prompt "Add to My Favorite"
Define Bar 2 Of PopFaves Prompt "Remove from My Favorite"
Define Bar 3 Of PopFaves Prompt "Show My Favorite"
On Selection Popup PopFaves Thisform._SubFave(Bar(), m.lcCaption)
Activate Popup PopFaves




Where _SubFave is a form method I created to receive parameters and handle calls of current selection.  And it just darn won't accept Thisform reference there!

And so on all popups I have, I have to use an outside prg for that with something like this:

* Calling as Procedure
Define Popup PopFaves From Mrow(),Mcol() Margin SHORTCUT
Define Bar 1 Of PopFaves Prompt "Add to My Favorite"
Define Bar 2 Of PopFaves Prompt "Remove from My Favorite"
Define Bar 3 Of PopFaves Prompt "Show My Favorite"
On Selection Popup PopFaves DO SubFave with Bar(), m.lcCaption  
Activate Popup PopFaves

or

* Calling as Function
Define Popup PopFaves From Mrow(),Mcol() Margin SHORTCUT
Define Bar 1 Of PopFaves Prompt "Add to My Favorite"
Define Bar 2 Of PopFaves Prompt "Remove from My Favorite"
Define Bar 3 Of PopFaves Prompt "Show My Favorite"
On Selection Popup PopFaves SubFave(Bar(), m.lcCaption) 
Activate Popup PopFaves


And then later finds a way to bring back focus from that Function/Procedure to the active form's methods because like I said I want to encapsulate "almost" everything to said form; which works, but not to my entire satisfaction.  However, when I need to do things, I need to do things fast and so I use the function/procedure calling way until I am free to experiment, which I do today

Playing with my popup for favorites (using right-click on treeview), through trials and errors, I found that we can do it like this:

Define Popup PopFaves From Mrow(),Mcol() Margin SHORTCUT
Define Bar 1 Of PopFaves Prompt "Add to My Favorite"
Define Bar 2 Of PopFaves Prompt "Remove from My Favorite"
Define Bar 3 Of PopFaves Prompt "Show My Favorite"
On Selection Popup PopFaves _Screen.ActiveForm._subfave(Bar(), m.lcCaption)
Activate Popup PopFaves

And it works, I can now use a form method for said popup

But then, _Screen.ActiveForm seems to still be outside of  a real encapsulation because in a way you are going out of the current form back to VFP's main screen (_screen), checking which form is currently active (current form) and then coming back inside the form to work on the form's method.  So here is a better way (for me) than that.  Put the reference to the current form in a local variable and use that variable on your need like this:

LOCAL loForm
loForm = thisform
Define Popup PopFaves From Mrow(),Mcol() Margin SHORTCUT
Define Bar 1 Of PopFaves Prompt "Add to My Favorite"
Define Bar 2 Of PopFaves Prompt "Remove from My Favorite"
Define Bar 3 Of PopFaves Prompt "Show My Favorite"
On Selection Popup PopFaves m.loForm._subfave(Bar(), m.lcCaption)
Activate Popup PopFaves

And there you have it.  A way to encapsulate the call to popups within the form itself.  Just in case you didn't know yet as well like I do before this.  Cheers!

No comments:

Post a Comment