Issues setting script-scoped variables within forms

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 1 year and 4 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
NorthernLightDE
Posts: 2
Last visit: Tue Nov 07, 2023 6:51 am

Issues setting script-scoped variables within forms

Post by NorthernLightDE »

Hello,
I'm rather new to Powershell Studio, so I'm not sure if this is a case of me doing something wrong, or just a nuance of the program. The end goal of what I am trying to do is to create a simple "Wizard-Style" gui, with back and next buttons. Clicking either button will set a variable, and in a loop, the next form will open based on that variable set. The issue I'm running into, I believe is with scoping but am unsure.

I've declared the needed variables as script-scoped (Also tried Global-scoped as a test and it didn't work...) in the default Globals.ps1 file that comes with the form project. Then in the forms load and button click events, I'm attempting to set them. Using debug mode and breakpoints I'm seeing that the variables aren't actually being set. Code snippets are below, and I've also attached a simplified test project showing the issue. In this exmaple, TESTVAR is always set to "start" and never changes off of it.

If anyone has ideas on what to try, or point me in the right direction, I'd appreciate it!

Globals.ps1:
  1. #--------------------------------------------
  2. # Declare Global Variables and Functions here
  3. #--------------------------------------------
  4. $script:TESTVAR = $null
  5. $script:EXIT = $null
MainForm.psf:
  1. $form1_Load={
  2.     #TODO: Initialize Form Controls here
  3.     $script:TESTVAR = "load"
  4. }
  5.  
  6. $buttonOK_Click={
  7.     #TODO: Place custom script here
  8.     $script:TESTVAR = "button"
  9. }
Startup.pss (Inside of the Main function):
  1.     $TESTVAR = "start"
  2.     while ($EXIT -ne $true) {
  3.         Show-MainForm_psf
  4.         Write-Output $TESTVAR
  5.        
  6.         If ($TESTVAR -eq "button") {
  7.             $EXIT = $true
  8.         }
  9.     }
Attachments
testing.zip
(15.48 KiB) Downloaded 27 times
User avatar
brittneyr
Site Admin
Posts: 1654
Last visit: Thu Mar 28, 2024 7:23 am
Answers: 39
Been upvoted: 30 times

Re: Issues setting script-scoped variables within forms

Post by brittneyr »

In your Startup.pss file, you need to change $TESTVAR to $script:TESTVAR like so:
  1.  $script:TESTVAR = "start"
  2.     while ($EXIT -ne $true) {
  3.         Show-MainForm_psf
  4.         Write-Output $script:TESTVAR
  5.        
  6.         If ($script:TESTVAR -eq "button") {
  7.             $EXIT = $true
  8.         }
  9.     }
Brittney
SAPIEN Technologies, Inc.
NorthernLightDE
Posts: 2
Last visit: Tue Nov 07, 2023 6:51 am

Re: Issues setting script-scoped variables within forms

Post by NorthernLightDE »

That took care of the issue, I figured it was something simple.

Thanks!
This topic is 1 year and 4 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