Powershell 变量替换-引用不起作用
我有这段代码来测试PowerShell中的GUI构建,我想通过创建一个函数来创建一个标签或按钮,从而简化以后添加更多的标签或按钮。 我想在这里测试的是,当我按下按钮1时,它会改变标签1的文本。但我不能让它工作。这是我的代码:Powershell 变量替换-引用不起作用,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 = 25
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\u Current.Add\u Click({$Label\u 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()
这可能是一个“聪明”的解决方案,而不是一个“易于理解和维护”的解决方案,但我还是添加了它,因为它使用了我在开始研究这个答案之前不知道的东西,即方法:
添加类型-assembly System.Windows.Forms
功能新标签
{
参数($LabelNum,$LabelText)
$label=新对象System.Windows.Forms.label
$label.Name=“label\u$LabelNum”
$label.Text=$LabelText
$LabelY=1+$LabelNum*50
$label.Location=新对象系统.Drawing.Point(10,$LabelY)
返回$label
}
新功能按钮
{
参数($ButtonNum,$ButtonText)
$button=新建对象System.Windows.Forms.button
$button.Text=$ButtonText
$Buttonny=1+$ButtonNum*50
$button.Location=新对象系统.Drawing.Point(150,$ButtonY)
返回$按钮
}
函数新形式
{
$form=新对象System.Windows.Forms.form
$form.Text=“Test1”
$form.Width=250
$form.Height=250
#创建标签数组
$labels=0..3 |%{
新标签-LabelNum$\ux-LabelText“标签”($\ux)”
}
#将标签添加到表单中
$labels |%{$form.Controls.Add($|)}
#创建一个按钮数组
$buttons=0..3 |%{
新按钮-按钮num$按钮文本“按钮”($)
}
#将按钮添加到表单中
$buttons |%{$form.Controls.Add($|)}
#在按钮上添加事件处理程序
对于($i=0;$i-lt$buttons.Length;$i++)
{
$buttons[$i]。添加\单击(
{$labels[$i].Text=$buttons[$i].Text}.GetNewClosure()
)
}
返回$form
}
$main_form=新表格
$main_form.ShowDialog()
在我看来,我已经做了一些事情,通过删除对函数中全局变量的引用来简化代码。函数不向$main_form
本身添加标签和按钮,而是返回一个独立的标签或按钮,并让调用代码决定如何处理它
“聪明”(但可能不是那么“聪明”)位使用newform
中的GetNewClosure
——首先我们创建标签和按钮的并行数组,然后将它们压缩在一起以创建事件处理程序
这样做的好处是,在创建标签和按钮后,您不需要再次按名称“查找”它们,因为您在数组中保留了对它们的引用,但缺点是它使用GetNewClosure
,这不容易解释它实际上在做什么
我认为这可能更实用,但这里有一些指向GetNewEnclosure
文档的链接,以防您想了解它
- devblogs.microsoft.com-
- rakhesh.com-
希望这有帮助…我解决了这样的问题:
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
$global:LabelArray = @()
function add-Label
{
param($LabelNum, $LabelText)
$Labelvar = New-Object System.Windows.Forms.Label
$LabelHeight = 1+ $LabelNum * 50
$dynamicVar = "Label_{0}" -f $LabelNum
$CurrentVar = New-variable -Name $dynamicVar -Value $Labelvar -PassThru -Force
$global:LabelArray += $CurrentVar
$global:LabelArray[$LabelNum].Value.Text = "$LabelText"
$global:LabelArray[$LabelNum].Value.Location = New-Object System.Drawing.Point(10,$LabelHeight)
$main_form.Controls.Add($global:LabelArray[$LabelNum].Value)
}
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)
switch($ButtonNum)
{
0 {$Button_Current.Add_Click({write-host $script:LabelArray[0].Value.Text})}
1 {$Button_Current.Add_Click({write-host $script:LabelArray[1].Value.Text})}
2 {$Button_Current.Add_Click({write-host $script:LabelArray[2].Value.Text})}
3 {$Button_Current.Add_Click({write-host $script:LabelArray[3].Value.Text})}
}
$main_form.Controls.Add($Button_Current)
}
add-Label -LabelNum 0 -LabelText "Test1"
add-Label -LabelNum 1 -LabelText "Test2"
add-Label -LabelNum 2 -LabelText "Test3"
add-Label -LabelNum 3 -LabelText "Test4"
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()
您的按钮仅更改最后一个标签,因为您的最后一个标签是“$label\u Current”的值。