我正在创建一个带有按钮的小的PowerShellGUI来快速运行各种PowerShellGUI命令。我已经得到了我想要的总体布局,但是现在我正在尝试通过使用按钮构建函数来简化我的代码。
我在一个单独的函数中设置各种各样的traits,比如“Button.Text”,“Button.SetToolTip”,或者“Button.Add_Click”,没有问题,但是我在一个独立的函数中设置“Button.Add_MouseEnter”和“Button.Add_MouseLeave”方面有问题。代码编译得很好,但是当我在GUI中将鼠标悬停在按钮上时,我得到了以下错误(第8-10行有类似的错误)
“在此对象上找不到属性”BackColor“。请验证该属性是否存在并且可以设置。位于第7行char:30 $tButton.Add_MouseEnter({$tButton.BackColor = '#990000'})类别信息:无效操作:(:)[],运行时异常完全限定错误ID:属性未找到”
我试着在函数中完全创建按钮对象并返回按钮,这将正确设置所有按钮特性,但当鼠标经过时仍然会给出相同的错误。我想我可能需要以不同的方式将按钮对象传递给函数/返回按钮(取决于第一码或第二码),但我不知道。我想也许可以用[CmdletBinding]绑定参数()]但错误仍然出现(我可能不太理解这一点)如有任何见解,将不胜感激
=====初始代码=====
'
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
Clear-Host
function Set-Hover ($tButton){
$tButton.Add_MouseEnter({$tButton.BackColor = '#990000'})
$tButton.Add_MouseEnter({$tButton.ForeColor = '#000000'})
$tButton.Add_MouseLeave({$tButton.BackColor = '#000000'})
$tButton.Add_MouseLeave({$tButton.ForeColor = '#FFFFFF'})
}
$Window = New-Object System.Windows.Forms.Form
$Window.ClientSize = New-Object System.Drawing.Size(100, 50)
$Global:Button = New-Object 'system.Windows.Forms.Button'
$Button.Size = New-Object System.Drawing.Point(75,40)
$Button.Location = New-Object System.Drawing.Point(5, 5)
$Button.text = "Button"
$Button.BackColor = '#000000'
$Button.ForeColor = '#FFFFFF'
Set-Hover -tButton $Button
$Window.Controls.Add($Button)
<# The code works when the Mouse_Enter/Leave lines are here, not in a function
$Button.Add_MouseEnter({$Button.BackColor = '#990000'})
$Button.Add_MouseEnter({$Button.ForeColor = '#000000'})
$Button.Add_MouseLeave({$Button.BackColor = '#000000'})
$Button.Add_MouseLeave({$Button.ForeColor = '#FFFFFF'})
#>
$Window.ShowDialog()
'=====第二个代码=====
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
Clear-Host
function Set-Hover {
$tButton = New-Object 'system.Windows.Forms.Button'
$tButton.Size = New-Object System.Drawing.Point(75,40)
$tButton.Location = New-Object System.Drawing.Point(5, 5)
$tButton.text = "Text"
$tButton.BackColor = '#FFFFFF'
$tButton.ForeColor = '#000000'
$tButton.Add_MouseEnter({$tButton.BackColor = '#990000'})
$tButton.Add_MouseEnter({$tButton.ForeColor = '#000000'})
$tButton.Add_MouseLeave({$tButton.BackColor = '#000000'})
$tButton.Add_MouseLeave({$tButton.ForeColor = '#FFFFFF'})
$tButton
}
$Window = New-Object System.Windows.Forms.Form
$Window.ClientSize = New-Object System.Drawing.Size(100, 50)
$Button1 = Set-Hover
$Window.Controls.Add($Button1)
<# The code works when the Mouse_Enter/Leave lines are here, not in a function
$Button.Add_MouseEnter({$Button.BackColor = '#990000'})
$Button.Add_MouseEnter({$Button.ForeColor = '#000000'})
$Button.Add_MouseLeave({$Button.BackColor = '#000000'})
$Button.Add_MouseLeave({$Button.ForeColor = '#FFFFFF'})
#>
$Window.ShowDialog()
1条答案
按热度按时间gmxoilav1#
您需要在表单代码中定义按钮,而不是在鼠标悬停在按钮上(或离开按钮)时在函数内部创建新按钮。
如果您确实希望为此使用函数,请更改您的函数以接受参数(第一个是按钮对象本身,第二个是用于确定它是MouseEnter还是MouseLeave事件的开关)
此外,最好不要再使用旧的
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
语法。以下是修改后的代码: