Page 1 of 1

Multiple Forms - Globals.ps1/$script:/$global:

Posted: Mon Dec 18, 2017 8:48 pm
by angelofstealth
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?

Re: Multiple Forms - Globals.ps1/$script:/$global:

Posted: Mon Dec 18, 2017 10:22 pm
by jvierra
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'

Re: Multiple Forms - Globals.ps1/$script:/$global:

Posted: Mon Dec 18, 2017 10:47 pm
by angelofstealth
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.

Re: Multiple Forms - Globals.ps1/$script:/$global:

Posted: Mon Dec 18, 2017 10:53 pm
by jvierra
How are you updating $childitem? What is the exact code?

Re: Multiple Forms - Globals.ps1/$script:/$global:

Posted: Mon Dec 18, 2017 11:04 pm
by angelofstealth
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')")

}

Re: Multiple Forms - Globals.ps1/$script:/$global:

Posted: Mon Dec 18, 2017 11:36 pm
by jvierra
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

Re: Multiple Forms - Globals.ps1/$script:/$global:

Posted: Tue Dec 19, 2017 1:46 pm
by angelofstealth
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.