powershell - 变量替换 - 引用不起作用

标签 powershell

我有这段代码来测试 PowerShell 中的 GUI 构建,我想通过创建一个创建标签或按钮的函数来缩短它,以便简化之后添加更多这些内容的过程。 我想在这里测试的是,当我按下按钮 1 时,它会更改标签 1 的文本。但我无法让它工作。这是我的代码:

Add-Type -assembly System.Windows.Forms
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = "Test1"
$main_form.Width = 250
$main_form.Height = 250

function add-Label
{
param($LabelNum, $LabelText)
$script:Label_Current = New-Variable -Name "Label_{$LabelNum}" -Value $LabelNum -Force -PassThru
$script:Label_Current = New-Object System.Windows.Forms.Label

$script:Label_Current.Name = "Label_$LabelNum"
$script:Label_Current.Text = "$LabelText"
$LabelHeight = 1+ $LabelNum * 50
$script:Label_Current.Location = New-Object System.Drawing.Point(10,$LabelHeight)

$main_form.Controls.Add($script:Label_Current)
}

function add-Button
{
param($ButtonNum, $ButtonText)
$Button_Current = New-Variable -name "Button_{$ButtonNum}" -Value $ButtonNum -Force -PassThru
$Button_Current = New-Object System.Windows.Forms.Button
$Button_Current.Text = "$ButtonText"
$ButtonHeight = 1+ $ButtonNum * 50
$Button_Current.Location = New-Object System.Drawing.Point(150,$ButtonHeight)
$Button_Current.Add_Click({$Label_Current.Text = "Test10"})
$main_form.Controls.Add($Button_Current)
}

add-Label -LabelNum 0 -LabelText "Test3"
add-Label -LabelNum 1 -LabelText "Test3"
add-Label -LabelNum 2 -LabelText "Test3"
add-Label -LabelNum 3 -LabelText "Test3"
add-Button -ButtonNum 0 -ButtonText "Test3"
add-Button -ButtonNum 1 -ButtonText "Test3"
add-Button -ButtonNum 2 -ButtonText "Test3"
add-Button -ButtonNum 3 -ButtonText "Test3"


$main_form.ShowDialog() 

当我现在运行它时,所有按钮只会更改最后添加的标签。 当我告诉它更改特定数字的文本 ($Button_Current.Add_Click({$Label_1.Text = "Test10"}) 时,它会显示“找不到对象”

提前致谢!

最佳答案

您可以使用表单自己的控件集合并使用标签控件名称,而不是尝试为标签和按钮创建新变量,这样就可以很容易地定位特定标签。

如下所示:

Add-Type -AssemblyName System.Windows.Forms

function Add-Label ([int]$index, [string]$labelText) {
    $top  = 1 + $index * 50
    $temp = New-Object System.Windows.Forms.Label
    $temp.Name = "Label$index"
    $temp.Text = $labelText
    $temp.Location = New-Object System.Drawing.Point(10,$top)

    $main_form.Controls.Add($temp)
}

function Add-Button ([int]$index, [string]$buttonText, [string]$newLabelText) {
    $top   = 1 + $index * 50
    $temp  = New-Object System.Windows.Forms.Button
    $temp.Text = $buttonText
    $temp.Name = "Button$index"
    $temp.Location = New-Object System.Drawing.Point(150,$top)
    # store the new text for the label in the button Tag
    # because inside the Click() method, $newLabelText is unknown
    $temp.Tag = $newLabelText   

    $temp.Add_Click({ 
        # get the index for the label from the buttons name
        $labelIndex = [int]($this.Name -replace '\D')
        # use the Find method to get the wanted label by name
        $main_form.Controls.Find("Label$labelIndex", $true)[0].Text = $this.Tag
    })

    $main_form.Controls.Add($temp)
}

$main_form = New-Object System.Windows.Forms.Form
$main_form.Text   = "Test1"
$main_form.Width  = 250
$main_form.Height = 250

# add the controls to the form
(0..3) | ForEach-Object { 
    Add-Label $_ "Test$_"
    Add-Button $_ "Button$_" "New Label Text $_" 
}

$main_form.ShowDialog() 

# don't forget to dispose of the form when done !
$main_form.Dispose()

关于powershell - 变量替换 - 引用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59926929/

相关文章:

powershell - UseBasicParsing不返回HTML表单

powershell - Get-AzureStorageBlob : Could not get the storage context. 请传入存储上下文或设置当前存储上下文

Powershell 点源在记事本中打开文件

powershell - 无法使用 PowerShell 从远程服务器访问环境变量

Powershell:下载文件 404 但站点存在

powershell - 这些 Cmdlet 之间有什么区别?

powershell - Azure Powershell 检查资源名称

sql-server - Powershell SMO Restore脚本的单元测试

c++ - 当前步骤 : Building Tasks. json 文件

powershell - 使用 PowerShell 在 Azure SQL 数据库中启用诊断