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

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.
Locked
User avatar
angelofstealth
Posts: 67
Joined: Wed Feb 13, 2013 4:26 pm

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

Post 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?

jvierra
Posts: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post 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'

User avatar
angelofstealth
Posts: 67
Joined: Wed Feb 13, 2013 4:26 pm

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

Post 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.

jvierra
Posts: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra »

How are you updating $childitem? What is the exact code?

User avatar
angelofstealth
Posts: 67
Joined: Wed Feb 13, 2013 4:26 pm

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

Post 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')")

}

jvierra
Posts: 14553
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post 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

User avatar
angelofstealth
Posts: 67
Joined: Wed Feb 13, 2013 4:26 pm

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

Post 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.

Locked