Referencing Variables across the events

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 7 years 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
User avatar
v_2nas
Posts: 16
Last visit: Sat Apr 10, 2021 1:29 am

Referencing Variables across the events

Post by v_2nas »

PowerShell Studio 2016 x64
Win 7 Pro SP 1 x64

Pardon my ignorance here as I am new to WFC, I wanted to know if variables can be called between the click events. Below are 2 click events from the powershell gui form.

Now when I debug the script, i notice that $invoicepath and $AllBusinessPartners would show as empty objects however in the scope of click event they do contain the values.

I understand that I can use $Script:variable to call the variables anywhere in the script however i want to have better understanding if the variables can be called as we use to do in regular ps scripts.

$buttonSelectInvoiceFolder_Click={
#TODO: Place custom script here

$folderbrowsermoderndialog1.Title = "Select Invoice Root Folder"
if ($folderbrowsermoderndialog1.ShowDialog() -eq 'OK')
{
$InvoicePath = $folderbrowsermoderndialog1.SelectedPath
$statusbar1.Text = "Invoice Root Folder: $($InvoicePath)"
}
}


$buttonParseInvoices_Click= {

#TODO: Place custom script here
$AllInvoices = @()
$AllInvoices = (Get-ChildItem $InvoicePath).Name

# Calling ParseInvoices Function and passing AllInvoices and AllBusinessPartners arrayObject
ParseInvoices -AllInvoices $AllInvoices -AllBusinessPartners $AllBusinessPartners
}
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Referencing Variables across the events

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Referencing Variables across the events

Post by jvierra »

You don't need a variable as you already have one:

Just reference the controls property as it will still be there.
  1. $buttonSelectInvoiceFolder_Click = {
  2.     $folderbrowsermoderndialog1.Title = "Select Invoice Root Folder"
  3.     if ($folderbrowsermoderndialog1.ShowDialog() -eq 'OK'){
  4.         $statusbar1.Text = "Invoice Root Folder: $($InvoicePath)"
  5.     }
  6. }
  7.  
  8. $buttonParseInvoices_Click = {
  9.     $AllInvoices = @()
  10.     $AllInvoices = (Get-ChildItem $folderbrowsermoderndialog1.SelectedPath).Name
  11.    
  12.     # Calling ParseInvoices Function and passing AllInvoices and AllBusinessPartners arrayObject
  13.     ParseInvoices -AllInvoices $AllInvoices -AllBusinessPartners $AllBusinessPartners
  14. }
User avatar
v_2nas
Posts: 16
Last visit: Sat Apr 10, 2021 1:29 am

Re: Referencing Variables across the events

Post by v_2nas »

are you saying that if i am using powershell studio inbuilt controls/functions then it's better to call them using the following way across the script rather than storing them in another variable and using that variable.

$InvoicePath = $folderbrowsermoderndialog1.SelectedPath

this leads to another questions, how about normal powershell variables, how do i ensure that i can use them anywhere in the script, including custom functions.
Try
{
$BPArchivesPath = New-Item -Name Archives -ItemType Directory -Path $TempStore -ErrorAction SilentlyContinue
if ($BPArchivesPath)
{
$Script:FNBPArchivesPath = $BPArchivesPath.BaseName
Write-Log -Message "$FNBPArchivesPath has been created at $TempStore" -level Info
}
}
Catch
{
Write-Log -Message $Error[0].Exception.Message -level Error
# StopCodeExecution
}

Unless i specify $Script infront of FNBPArchivesPath, i can't use them in function or click events.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Referencing Variables across the events

Post by jvierra »

An object is a variable and it stores information. That is why we use them. You do not need to copy the information into a local variable to sue it. This is a basic strenght of objects and object systems.

In programming we pass variable to functions and return results. That is the primary model of a program. Other issues need to be addressed individually. Do you have a specific situation that you are referring to?
User avatar
v_2nas
Posts: 16
Last visit: Sat Apr 10, 2021 1:29 am

Re: Referencing Variables across the events

Post by v_2nas »

I read this post https://www.sapien.com/blog/2013/04/10/ ... revisited/ as per it, if we need to access variable across different events, we need to use scope the variable using script. Is that correct?

My script would only work if use define scope as script for the variable otherwise, it fails to return the data.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Referencing Variables across the events

Post by jvierra »

If you must use a variable then it must be defined in an outer scope that is visible where needed. The ususal is "script" scope. My only caution is that you not use scoped variables when a control already retains the data. This can lead to hard to debug issues.
User avatar
v_2nas
Posts: 16
Last visit: Sat Apr 10, 2021 1:29 am

Re: Referencing Variables across the events

Post by v_2nas »

Actually, I am using the same variable across the script, for example, I create a variable and then i use it at different places in the script without having to prefix it with $script.
When you say defined a variable in outscope, do you mean, outside the form function? and do you mean all the variables?

Actually i prefer to use the variable without having to define $script, as it make it bit easier to port powershell script code into psf. Let me know if you would like to see the code. it's bit chunky though.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Referencing Variables across the events

Post by jvierra »

Variables declared in an event are not available outside of the event if the are not "scoped" as "script"/

Variables defined outside of an event are not changeable in an event without using "script". This is true in forms and in all PowerShell scripts.

You cannot avoid the scope requirements. It is not something you can just choose to ignore.

Controls are objects declared at the level of the function. Objects properties can be read and set anytime the object is visible even if defined in an outer scope.

These things are a foundation of object programming (OOP) which PowerShell follows. PowerShell is an OOP language.

First read about PowerShell variables then about scopes. PSF is nothing more than an XML file used to generate a PS1 file when you execute or debug the PSF. All PowerSHell rules always apply.

See:
https://technet.microsoft.com/en-us/lib ... 47734.aspx

https://technet.microsoft.com/en-us/lib ... 47849.aspx
User avatar
v_2nas
Posts: 16
Last visit: Sat Apr 10, 2021 1:29 am

Re: Referencing Variables across the events

Post by v_2nas »

Thanks for the explaining it patiently. It makes much more sense now when working with PS 2016.
This topic is 7 years 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