Form Scoping Woes

Hello Ninja’s,

I have a problem. I am trying to get a function to execute inside of a button click event. I would expect that if it was a scope issue, that it wouldn’t event affect the target (in this case, a list box), however, it does! But it comes back with a blank entry addition. Here is a sample to demonstrate what I am talking about.

Add-type -AssemblyName System.Windows.Forms,System.Drawing

$Main = [Windows.Forms.Form]@{
    ClientSize = "1024,768"
    Text = "AGDLP Master-Tron-O-Matic 9000+"
    MinimumSize = "800,600"

$Script:LogBox = [Windows.Forms.ListBox]@{
    Enabled = $True
    Size = "1004,190"
    Location = "10,568"
    Anchor = "Bottom,Top,Left,Right"
    SelectionMode = "MultiExtended"

Function AddLogEntry {
    Param (
    $VisibleItems = $Script:LogBox.ClientSize.Height / $Script:LogBox.ItemHeight
    $Script:LogBox.TopIndex = [Math]::Max(($Script:LogBox.Items.Count - $VisibleItems + 1), 0)


$TestButton = [Windows.Forms.Button]@{
    Text = "Text Log Entry"
    Size = "120,20"
    Location = "10,10"

$TestButton_Click = {
    AddLogEntry -Input "This is a random number: $(Get-Random 1000000)"
    $Script:LogBox.Items.Add("This is a random number: $(Get-Random 1000000)")




Does anyone know what is going on here? I have been beating my head over this for awhile.

Nevermind, I solved it. $Input already exists in all powershell sessions. Once I changed the parameter in AddLogEntry to a unique variable name, everything worked fine.