Forms Problem ( Using Powershell Studio)

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
kryten
Posts: 45
Last visit: Fri Dec 25, 2020 7:46 am

Forms Problem ( Using Powershell Studio)

Post by kryten »

Hi,

I'm a very happy user of Powershell Studio. Awesome software!

I've been experimenting with multi form projects with some success having built some very useful applications but I have a problem with one experimental project that is beginning to drive me nuts and would appreciate some help with, if possible.

In nutshell my application is a password manager. Works very well too, apart from one issue:

The mainform shows all the usual fields including a combobox for the 'category' of account, textboxes for the username of the selected account and password (obscured and encrypted!) all the usual stuff you would see in a password manager.

There is an "Add" button which brings up the childform where the user can enter all the data associated with the new entry. The childform has two buttons: Submit and Quit.

The Submit button immediately writes the childform data into the file the $script:OnLoadFormEvent in the mainform reads when the application starts. And I can see that this does indeed happen by monitoring the data store file after clicking "Submit".

The issue is when clicking Quit. What I need to happen is for the $script:OnLoadFormEvent to fire and cause the form to reload as if freshly launched so that the new entry is now visible in the mainform combobox and available to be selected. In the scriptblock associated with quit I simply have this:-

$buttonQuit_Click={
$formAddAnAccount.Close()
&$script:OnLoadFormEvent
}

To close the childform and reload the mainform. I can "see" the $OnLoadFormEvent fire because I can put controls in that block to write-host "Called by $buttonQuit" and so on. But the form does not reload as expected as the newly added entry is not visible.

It doesn't matter whether I close the childform then call &$script:OnLoadFormEvent or whether I call &$script:OnLoadFormEvent then close the childform. None of that makes any difference. I've tried introducing timing, multiple calls to refresh the forms etc nothing so far has produced the effect of cleanly reloading the mainform from the childform.

What I find perplexing is that to temporarily overcome the problem I added a button to the mainform and called it "reload data". All that button does is call $script:OnLoadFormEvent (nothing else) yet it produces precisely the effect I am looking for. The form reloads from scratch and all the data, including the new account is visible.

My next step would probably be to desconstruct the code in the &$script:OnLoadFormEvent into other blocks/functions in the hope of slicing out the problem but that would take a wee while.

Clearly I am missing something. Probably something daft too. But I haven't been able to put my finger on it yet.

Appreciate it's a bit of a tricky one to try to explain never mind to try to comprehend without seeing it, would be happy to post/email the source if that would help?

Thanks for listening - any suggestions appreciated!

Stuart
User avatar
kryten
Posts: 45
Last visit: Fri Dec 25, 2020 7:46 am

Forms Problem ( Using Powershell Studio)

Post by kryten »

In desperation I cut all the code out of all the forms and saved the project.

Then pasted it all back in, one block at a time, saved it and it works as expected.
My head hurts.
Stuart
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Forms Problem ( Using Powershell Studio)

Post by davidc »

I don't recommend calling an event of another form from another. PowerShell scoping can be finicky when it comes to events. Instead I recommend you call the refresh from the main form:
First, assign a DialogResult to the quit button and update the code when you call the form:

If((Call-ChildForm) –eq ‘OK’)
{
RefreshSettings()
}

David
David
SAPIEN Technologies, Inc.
User avatar
kryten
Posts: 45
Last visit: Fri Dec 25, 2020 7:46 am

Forms Problem ( Using Powershell Studio)

Post by kryten »

Hi David,

Appreciate the response.

The technique you are alluding to looks very useful. Forgive my ignorance but would it be possible to explain it a little more fully?

I set the DialogResult to "OK" on the quit button but I'm not following how we use that to tell the mainform that the child has just closed.

Understand completely if you are too busy though.

Cheers,
Stuart
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Forms Problem ( Using Powershell Studio)

Post by davidc »

The following spotlight article might help:

http://www.sapien.com/blog/2011/06/07/p ... n-control/

The set DialogResult value is returned by the Call function. Depending on what value you assign, you can check for it using the return value.

David
David
SAPIEN Technologies, Inc.
User avatar
kryten
Posts: 45
Last visit: Fri Dec 25, 2020 7:46 am

Forms Problem ( Using Powershell Studio)

Post by kryten »

Thanks David,

I've been playing around with the DialogResult property and it definitely seems to be way more robust. Great tip!

I'm now using a switch statement to capture the return value from the child form call, along the lines of:-

$buttonCallChild_Click={

switch (Call-ChildForm_pff) {
"OK" { #Quit Button Clicked
"Child closed with OK returned by DialogResult"
}
"Cancel" { #Cancel Button Clicked
"Child closed with Cancel returned by DialogResult"
}
}
}

And it's working perfectly.

Stuart
User avatar
Srinath
Posts: 111
Last visit: Tue Feb 09, 2016 12:12 pm

Forms Problem ( Using Powershell Studio)

Post by Srinath »

I ran into similar situation and the solution works like a charm!
This topic is 11 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.