Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 展开“添加”中的所有变量,然后单击“函数内部”_Winforms_Powershell - Fatal编程技术网

Winforms 展开“添加”中的所有变量,然后单击“函数内部”

Winforms 展开“添加”中的所有变量,然后单击“函数内部”,winforms,powershell,Winforms,Powershell,它现在可以工作了,这要归功于 这让我更容易管理事情了你不知道你帮了多少忙 约2000行代码变成了约50行代码,我不必每次都重新生成50行代码,我现在想制作一个按钮我认为您的问题与此相关,您在调用DrawButton后在本地声明$button,而不是在全局声明live。 不幸的是,PowerShell没有强制声明的好方法 此外,您可能有不正确的类型。 $ButtonType已初始化为字符串,但您可以使用它ButtonType.Add\u单击作为按钮 无论如何,学习如何调试您的代码(例如使用Visu

它现在可以工作了,这要归功于

这让我更容易管理事情了你不知道你帮了多少忙


约2000行代码变成了约50行代码,我不必每次都重新生成50行代码,我现在想制作一个按钮

我认为您的问题与此相关,您在调用DrawButton后在本地声明$button,而不是在全局声明live。 不幸的是,PowerShell没有强制声明的好方法

此外,您可能有不正确的类型。 $ButtonType已初始化为字符串,但您可以使用它ButtonType.Add\u单击作为按钮


无论如何,学习如何调试您的代码(例如使用Visual Studio代码或PowerGui),您将能够自己找到答案。

我认为您的问题与您在调用DrawButton后在本地声明$button而不是在全局声明为live有关。 不幸的是,PowerShell没有强制声明的好方法

此外,您可能有不正确的类型。 $ButtonType已初始化为字符串,但您可以使用它ButtonType.Add\u单击作为按钮


无论如何,学习如何调试代码(例如,使用Visual Studio代码或PowerGui),您将能够自己找到答案。

您的问题是,事件处理程序在脚本范围的子范围内运行,而不是在函数范围内运行,因此它看不到
$Temp
变量。 换句话说:没有任何闭包可以捕获
$Temp
的值并使其在事件处理程序中可用

虽然可以使用作用域说明符
$script:
在脚本作用域中设置变量,但这通常是要避免的,并且在您的情况下,不会为每次调用
DrawButton
提供特定的单独值

最好使用
addmember
,将所需值作为自定义属性附加到按钮对象:

$TextApp1 = "it works"

function DrawButton {

    $Temp = "$args"

    $ButtonType = Get-Variable -Name "Button$Temp" -Value

    $ButtonType | Add-Member -NotePropertyMembers @{ DataTemp = $Temp }

    $ButtonType.Add_Click({
        $Data = $($this.DataTemp)
        $DefaultForm.Text = "$Data"
    })

    $DefaultGroupBox.Controls.Add($ButtonType)

}

DrawButton App1

您的问题是,事件处理程序在脚本作用域的子作用域中运行,而不是在函数的作用域中运行,因此它看不到
$Temp
变量。 换句话说:没有任何闭包可以捕获
$Temp
的值并使其在事件处理程序中可用

虽然可以使用作用域说明符
$script:
在脚本作用域中设置变量,但这通常是要避免的,并且在您的情况下,不会为每次调用
DrawButton
提供特定的单独值

最好使用
addmember
,将所需值作为自定义属性附加到按钮对象:

$TextApp1 = "it works"

function DrawButton {

    $Temp = "$args"

    $ButtonType = Get-Variable -Name "Button$Temp" -Value

    $ButtonType | Add-Member -NotePropertyMembers @{ DataTemp = $Temp }

    $ButtonType.Add_Click({
        $Data = $($this.DataTemp)
        $DefaultForm.Text = "$Data"
    })

    $DefaultGroupBox.Controls.Add($ButtonType)

}

DrawButton App1

您可以添加一个引用,解释什么是$DefaultForm、$DefaultGroupBox吗?
$DefaultForm=New Object System.Windows.Forms.Form
$DefaultGroupBox=New Object System.Windows.Forms.GroupBox
您可以添加一个引用,解释什么是$DefaultForm,$DefaultGroupBox?
$DefaultForm=新对象系统.Windows.Forms.Form
$DefaultGroupBox=新对象系统.Windows.Forms.GroupBox
$ButtonType
未初始化为字符串;它被设置为名为
“Button$Temp”
的预先存在的变量的值,该变量被假定为WinForms Button对象(即使OP的代码没有显式显示)。
$ButtonType
是一个局部变量与问题无关,因为它仅用于操作引用的对象。
$ButtonType
未初始化为字符串;它被设置为名为
“Button$Temp”
的预先存在的变量的值,该变量被假定为WinForms Button对象(即使OP的代码没有显式显示)。
$ButtonType
是一个局部变量,与问题无关,因为它只用于操作引用的对象。非常有效谢谢,在单击事件中重置我的所有变量有点奇怪,但非常好谢谢,非常有效谢谢,不得不在点击事件中重置所有变量有点奇怪,但非常好,谢谢