2

This is probably so simple, but I cannot figure it out.

I am getting the system date and storing it into a variable then doing a minus 1 on the day on a button click. Problem is, the cmdlet refreshes on each click, and it does not continue to decrement. So if today is the 21st, the button click will output 20, then if I click again it should output 19 but it always outputs 20.

[int] $myDD = (Get-Date -Format dd)

button1_click= { 
     $myDD = $myDD - 1
     write-host $myDD
}

Full Code

[int] $myDD = (Get-Date -Format dd)

function Create-Form {

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$form1 = New-Object System.Windows.Forms.Form
$button4 = New-Object System.Windows.Forms.Button 

#################################
#             CLICKS            #
#################################
$button4_Click= {
    $myDD = $myDD - 1
    write-host $myDD
}

$OnLoadForm_StateCorrection={
    $form1.WindowState = $InitialFormWindowState
}

#################################
#             FORM              #
#################################
$form1.Text = "Test"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 280
$System_Drawing_Size.Height = 100
$form1.ClientSize = $System_Drawing_Size

#################################
#            BUTTONS            #
#################################

#button4
$button4.TabIndex = 4
$button4.Name = "button4"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 20
$System_Drawing_Size.Height = 15
$button4.Size = $System_Drawing_Size
$button4.UseVisualStyleBackColor = $True
$button4.Text = "<<"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 245
$System_Drawing_Point.Y = 10
$button4.Location = $System_Drawing_Point
$button4.DataBindings.DefaultDataSourceUpdateMode = 0
$button4.Font = New-Object System.Drawing.Font("Lucida Console",12)
$button4.TextAlign.ContentAlignment.TopLeft
$button4.add_Click($button4_Click)
$form1.Controls.Add($button4)

<#-------------------------------#>

$InitialFormWindowState = $form1.WindowState
$form1.add_Load($OnLoadForm_StateCorrection)
$form1.ShowDialog()| Out-Null

}
Create-Form
mklement0
  • 382,024
  • 64
  • 607
  • 775
arealhobo
  • 447
  • 1
  • 6
  • 17
  • 1
    In short: `$myDD` was created in the _script_ scope; the event-handler code runs in a _child scope_ that can _get_ the script-level variable with `$myDD`, but _setting_ `$myDD` creates a _new, local_ variable, as explained in the [linked answer](https://stackoverflow.com/a/55403663/45375). The accepted answer here shows the right solution via the `$script:` scope specifier. – mklement0 Mar 22 '20 at 07:52

1 Answers1

2

After showing your full code, it looks like you can do:

button1_click= {
     $script:myDD--
     write-host $myDD
}

Which decrements the variable at Script scope. You can have a look at about_scopes for more information.

Note: Its best practice here to use Script level scoping with $script:myDD--, since the variable is defined in the scope of the script that is running. Using Global scoping has lingering side effects even after the script has run, so be wary when using this scoping level.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75