I want to be able to access a variable between the main form and child form.
1) if i declare a variable in globals.ps1 and update this variable in the main form i can return the value. If i show the child form and call the value it is empty (using show message box).
2) if i declare a global variable on the main form or within globals.ps1 the same thing happens like number 1. Value is empty on the child form.
When dealing with multiple forms and wanting to access a variable across forms what is the method or place i should be declaring the variable on?
Multiple Forms - Globals.ps1/$script:/$global:
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.
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.
- angelofstealth
- Posts: 69
- Last visit: Wed Sep 28, 2022 10:04 am
Re: Multiple Forms - Globals.ps1/$script:/$global:
From the posted information there is no way to understand you issue.
How are you assigning the value to the global variable? To assign a global outside of "globals.ps1" you must reference the global using its scope moniker:
$global:myvar = 'some value'
How are you assigning the value to the global variable? To assign a global outside of "globals.ps1" you must reference the global using its scope moniker:
$global:myvar = 'some value'
- angelofstealth
- Posts: 69
- Last visit: Wed Sep 28, 2022 10:04 am
Re: Multiple Forms - Globals.ps1/$script:/$global:
No problem, will explain further...
I declare a variable $Childitems = @() in the globals.ps1 , in the Main form i update $Childitems with values. I know $Childitems is getting updated with the values by using a show messagebox to display the results. I want to be able to click on a child form and the child form populates the values from the $Childitems. In the Child form in the form load i just put a message box prompt to show the values to see if it is receiving them and its blank. I did this as the $Childitems would not update/appending to a checkbox.
I declare a variable $Childitems = @() in the globals.ps1 , in the Main form i update $Childitems with values. I know $Childitems is getting updated with the values by using a show messagebox to display the results. I want to be able to click on a child form and the child form populates the values from the $Childitems. In the Child form in the form load i just put a message box prompt to show the values to see if it is receiving them and its blank. I did this as the $Childitems would not update/appending to a checkbox.
- angelofstealth
- Posts: 69
- Last visit: Wed Sep 28, 2022 10:04 am
Re: Multiple Forms - Globals.ps1/$script:/$global:
I have a job-tracker run a routine and in the completedscript section i update the $Childitems with the results, to make sure it is working i show one column of the data to confirm. This is displaying the correct information. I then have a button to load the child form, to test that $Childitems still has the correct value, i did another message box and it was correct. When the child form loads i try to update the child form with $Childitems and doesn't seem to work. As you see i try to use message box to see where the value is being lost, doesn't seem to get the value within the Child form.
$completedscript - Main Form:
$Childitems = $results.'SQLcmdCheckAuthType'
[System.Windows.Forms.MessageBox]::Show("$($Childitems.'AuthenticationTypeId')")
Child form:
$ChildformEnterPartnerIdpId_Load={
#TODO: Initialize Form Controls here
[System.Windows.Forms.MessageBox]::Show("$($Childitems.'AuthenticationTypeId')")
foreach ($Role1 in $Childitems)
{
Update-ComboBox $combobox1 $Role1.'AuthenticationTypeId' -Append
[System.Windows.Forms.MessageBox]::Show("$($Role1.'AuthenticationTypeId')")
}
$completedscript - Main Form:
$Childitems = $results.'SQLcmdCheckAuthType'
[System.Windows.Forms.MessageBox]::Show("$($Childitems.'AuthenticationTypeId')")
Child form:
$ChildformEnterPartnerIdpId_Load={
#TODO: Initialize Form Controls here
[System.Windows.Forms.MessageBox]::Show("$($Childitems.'AuthenticationTypeId')")
foreach ($Role1 in $Childitems)
{
Update-ComboBox $combobox1 $Role1.'AuthenticationTypeId' -Append
[System.Windows.Forms.MessageBox]::Show("$($Role1.'AuthenticationTypeId')")
}
Re: Multiple Forms - Globals.ps1/$script:/$global:
To assign to a global you must use the "global" scope specifier as I posted earlier. Without it the value assigned will create a new local variable that will disappear once it goes out of scope.
help about_scopes
help about_scopes
- angelofstealth
- Posts: 69
- Last visit: Wed Sep 28, 2022 10:04 am
Re: Multiple Forms - Globals.ps1/$script:/$global:
I made the scope change to global.ps1 and at first didn't work for me on the child form. Decided to create a new child form and test again and it worked.
Thanks for the help again jvierra, happy holidays.
Thanks for the help again jvierra, happy holidays.