powershell - .add_Click({}) 的范围问题

标签 powershell user-interface click

我尝试将功能动态分配给动态创建的按钮。不幸的是,变量没有像我预期的那样传递到 .add_Click({}) block 。

我已经尝试创建一个全局变量 $Global:Var,或者在哈希表中输入该变量,或者创建一个我在 click 语句中调用的函数。但是,最多我只得到列表中的最后一项。

如何将变量动态分配给 $var.add_Click({}) block ?

我的代码:

Foreach ($Tenant in $Customer){
    if (-not ($Tenant -eq "")){
        $Tenant_Button = New-Object -TypeName System.Windows.Forms.Button
        $Tenant_Button.Location = New-Object -TypeName System.Drawing.Size(5, (15 + ($Tenant_Counter++ * 40)))
        $Tenant_Button.Size = New-Object -TypeName System.Drawing.Size(125, 35)
        $Tenant_Button.Text = $Tenant
        $Tenant_Button.Font = $Font_AccountButton

#Below starts the .add_Click({}) part!#
#Above variables are not accessible in the below part. 

        $Tenant_Button.add_Click({
            $Customer_Domain = ($CsvImport | Where-Object {$_.command_name -match "Admin $($Tenant_Button.Text)"}).command_name
            $DomainCounter = 0
            Foreach ($Domain in $Customer_Domain){
                $Button_Cust = New-Object -TypeName System.Windows.Forms.Button
                $Button_Cust.Location = New-Object -TypeName System.Drawing.Size(545, (25 + ($DomainCounter++ * 40)))
                $Button_Cust.Size = New-Object -TypeName System.Drawing.Size(125, 35)
                $Button_Cust.Text = $Domain

                $MainWindow.Controls.Add($Button_Cust)

                $Button_Cust.add_Click({
                    $FunctionIndex = [array]::IndexOf(($CsvImport).command_name, $Domain)
                    $Customer_Function = ($CsvImport[$FunctionIndex]).object_command

                    Invoke-Expression -Command $Customer_Function
                })
            }
         })
         $GroupBox_Acc.Controls.Add($Tenant_Button)

如前所述,使用全局变量或函数或哈希表解决方案,我设法输入了一个变量内容,但总是只输入最后一个。因此,所有按钮都分配了相同的功能。 如何为二十个不同的按钮分配二十个不同的功能?

为了完成示例,我提供了相关代码。在 $Button_Accounts.add_Click({})(可以在下面代码的末尾找到)命令中输入“有问题的”代码。

$CsvImport = Import-Csv -Delimiter ',' -LiteralPath 'C:\Test\Coding\commands.csv'

$MainWindow = New-Object -TypeName System.Windows.Forms.Form
$MainWindow.Text = 'Administrator Window'
$MainWindow.Width = 600
$MainWindow.Height = 555
$MainWindow.AutoSize = $true

$Button_Accounts = New-Object -TypeName System.Windows.Forms.Button
$Button_Accounts.Location = New-Object -TypeName System.Drawing.Size(25, 225)
$Button_Accounts.Size = New-Object -TypeName System.Drawing.Size(200, 75)
$Button_Accounts.Text = 'Accounts'

    $MainWindow.Controls.Add($Button_Accounts)

$ComboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$GroupBox_Acc = New-Object -TypeName System.Windows.Forms.GroupBox

$Customer = $CsvImport.customer | Select-Object -Unique

$GroupBox_Acc.Text = 'Tenant List:'
$GroupBox_Acc.Location = New-Object -TypeName System.Drawing.Point(250, 25)
$GroupBox_Acc.Size = New-Object -TypeName System.Drawing.Size(270, 500)

$MainWindow.Controls.Add($GroupBox_Acc)

$Button_Accounts.Add_Click({
## Add above code here ##
})
$MainWindow.ShowDialog()

csv 内容示例:

command_group, Customer, command_name, object_commmand
Accounts, AAAA, Add Admin AP account, add_AP_admin_account
Accounts, AAAA, Add Admin AP Local account, add_AP_Local_admin_account
Accounts, BBBB, Add Admin ARL G account, add_ARLG_admin_account
Accounts, BBBB, Add Admin ARL CO account, add_ARLCO_admin_account

最佳答案

那么让我们先谈谈为什么它不起作用。

脚本 block {} 使用运行前调用的变量的最后一个值。

@("RED","BLUE","GREEN") | %{
    $Team = $_
    $Button.add_Click({
        [System.Windows.MessageBox]::Show($Team)
    })
}
$MainWindow.ShowDialog()

无论我点击红色或蓝色按钮,对话框总是会弹出绿色。它仍然只会弹出绿色。那是因为当发送 Click 的事件时,它采用变量 $Team 的最后一个值。

所以修复是 GetNewClosure()

@("RED","BLUE","GREEN") | %{
    $Team = $_
    $Button.add_Click({
        [System.Windows.MessageBox]::Show($Team)
    }.GetNewClosure())
}
$MainWindow.ShowDialog()

脚本 block {} 上的 GetNewClosure() 所做的是获取变量的当前值并将它们与脚本 block 一起存储。因此,如果变量发生变化,它不会影响存储在脚本 block 中的变量。

上面的代码在单击红色时返回红色,在单击蓝色时返回蓝色,在单击绿色时返回绿色。

关于powershell - .add_Click({}) 的范围问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56428816/

相关文章:

powershell - 在Powershell中使用主机名作为变量创建if语句时出错

git - PowerShell,vim和别名与* .cmd(使用git时)

java - JFrame 调整大小时显示时钟

ios - 单击按钮时退出应用程序 - iOS

jquery - 如何使用 jQuery 更改点击时的背景颜色?

powershell - 等于运算符的初学者问题

PostBuild 中的 PowerShell 脚本

java - 在 Swing 中实现拒绝用户更改值的最佳方法?

javascript - 以编程方式更改 EditorGrid 的单元格值

javascript - jQuery 函数在单击之前启动,为什么?