Returning ChildForm values in MainForm variable!

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

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 4 years and 11 months 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.
Locked
Taras_ua
Posts: 2
Last visit: Sat Apr 27, 2019 3:04 pm

Returning ChildForm values in MainForm variable!

Post by Taras_ua »

NEED HELP!!!!


Work in PS studio 2017
  1. $buttonCallChildForm_Click={
  2.    
  3.     if((Show-ChildForm_psf) -eq 'OK')
  4.     {
  5.         $LocAdmin = $ChildForm_SetAdmin
  6.         $LocPassword = $ChildForm_SetPass
  7.         $errorMsg = "admin -  " + $LocAdmin + ",  password= " + $LocPassword
  8.              $caption = "PassWord ERROR"
  9.             [System.Windows.Forms.MessageBox]::Show($errorMsg, $caption)
  10. #Returning values and we see it!
  11.     }
  12.  
  13.  
  14. #but in MainForm no changes
  15. $buttonCheck1_MouseDown=[System.Windows.Forms.MouseEventHandler]{
  16.  
  17.     $errorMsg = "admin -  " + $LocAdmin + ",  password= " + $LocPassword
  18.     $caption = "PassWord ERROR"
  19.     [System.Windows.Forms.MessageBox]::Show($errorMsg, $caption)
  20. }
  21. }  
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Returning ChildForm values in MainForm variable!

Post by jvierra »

Setting a variable in an event only defines the variable in the event. YOU cannot retain the value. It works the same for a function. Variables defined in a function or set in a function do not exists after the function ends. This is tru or all PowerShell scripts.

To understand this read the following; https://docs.microsoft.com/en-us/powers ... wershell-6
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Returning ChildForm values in MainForm variable!

Post by davidc »

PowerShell Studio automatically generates return variables for the child form controls. It uses the following naming convention for the return variables:

$ChildFormName_ControlName

The easiest way to determine if the variable is correct is by hovering over it. If PowerShell Studio displays the type information, then you have to correct variable name. You can also use PowerShell Studio's PrimalSense. Press $ and the return variables will be included in the variable list.

Here is a related article (The function verbs used are outdated):

https://info.sapien.com/index.php/guis/ ... sing-forms
David
SAPIEN Technologies, Inc.
Taras_ua
Posts: 2
Last visit: Sat Apr 27, 2019 3:04 pm

Re: Returning ChildForm values in MainForm variable!

Post by Taras_ua »

mr. jvierra
PowerShell Studio automatically generates return variables for the child form controls. And I use it/

https://info.sapien.com/index.php/guis/ ... sing-forms
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Returning ChildForm values in MainForm variable!

Post by jvierra »

Yes - I know that but that was not your original question. The variables defined in the child form are available in the calling form as defined but it you assign them to a new variable in an event then the new variable will not be available outside of the event. Just use the child defined variables. There is no need to redefine them.

You should not do this:

$LocAdmin = $ChildForm_SetAdmin

It is unnecessary and the new variable will not be visible to the whole form.
This topic is 4 years and 11 months 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.
Locked