powershell - 为什么这个 powershell 代码片段不起作用?
问题描述
这个特殊的powershell代码对我不起作用,我想知道我在这里做错了什么(我正在学习Powershell - 我对它很陌生)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = "Testing"
$main_form.Width = 500
$main_form.height = 300
$main_form.FormBorderStyle = "FixedDialog"
$main_form.AutoSize = $true
$main_form.MaximizeBox = $false
$main_form.MinimizeBox = $false
#
$SetCheckBox = New-Object System.Windows.Forms.Checkbox
$SetCheckBox.Location = New-Object System.Drawing.Point(10,10)
$SetCheckBox.Size = New-Object System.Drawing.Size(60,20)
$SetCheckBox.Text = "Set"
$main_form.Controls.Add($SetCheckBox)
if($SetCheckBox.Checked -eq $true){
$SetButton = New-Object System.Windows.Forms.Button
$SetButton.Location = New-Object System.Drawing.Point(280,200)
$SetButton.Size = New-Object System.Drawing.Size(100,20)
$SetButton.Text = "Nothing Selected"
$SetButton.AutoSize = $true
$main_form.Controls.Add($SetButton)
}else{
}
$RefreshButton = New-Object System.Windows.Forms.Button
$RefreshButton.Location = New-Object System.Drawing.Point(400,285)
$RefreshButton.Size = New-Object System.Drawing.Size(100,20)
$RefreshButton.Text = "Refresh"
$RefreshButton.AutoSize = $true
$main_form.Controls.Add($RefreshButton)
$RefreshButton.Add_Click({
$main_form.Refresh()
})
#
$main_form.Topmost = $true
$main_form.ShowDialog()
我希望它在“$SetCheckBox.Checked”等于 $true“时向我的表单添加一个按钮,但没有任何反应。没有错误代码或任何东西。也许这不是 Powershell 的工作方式?
解决方案
问题是您已经完成了所有设置,只是位置不正确。您需要在按钮按下时包含 if 语句才能对其进行操作。我不需要输入Refresh()
,但我只在 ISE 中进行了测试。这目前对我有用:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = "Testing"
$main_form.Width = 500
$main_form.height = 300
$main_form.FormBorderStyle = "FixedDialog"
$main_form.AutoSize = $true
$main_form.MaximizeBox = $false
$main_form.MinimizeBox = $false
$SetCheckBox = New-Object System.Windows.Forms.Checkbox
$SetCheckBox.Location = New-Object System.Drawing.Point(10,10)
$SetCheckBox.Size = New-Object System.Drawing.Size(60,20)
$SetCheckBox.Text = "Set"
$main_form.Controls.Add($SetCheckBox)
$RefreshButton = New-Object System.Windows.Forms.Button
$RefreshButton.Location = New-Object System.Drawing.Point(400,285)
$RefreshButton.Size = New-Object System.Drawing.Size(100,20)
$RefreshButton.Text = "Refresh"
$RefreshButton.AutoSize = $true
$main_form.Controls.Add($RefreshButton)
$RefreshButton.Add_Click({
if($SetCheckBox.Checked -eq $true){
Write-Host "testing"
$SetButton = New-Object System.Windows.Forms.Button
$SetButton.Location = New-Object System.Drawing.Point(280,200)
$SetButton.Size = New-Object System.Drawing.Size(100,20)
$SetButton.Text = "Nothing Selected"
$SetButton.AutoSize = $true
$main_form.Controls.Add($SetButton)
}else{
Write-Host "testing2"
}
})
$main_form.Topmost = $true
$main_form.ShowDialog()
推荐阅读
- node.js - 如何在依赖部分的 package.json 文件中添加环境变量?
- php - 在 Woocommerce 中退款时不要更改订单总额
- javascript - 如何在反应中在地图上显示来自图像 URl 的图像
- git - 无法配置 git 终端来推送已创建的项目
- html - 如何在 CSS 中创建斜线
- hyperledger-fabric - 来自 getHistoryForKey() 的事务 id 在 Hyperledger Fabric 中始终未定义
- magento2 - magento 2上注册和结帐页面上的电子邮件验证
- python - conda env 变量没有按预期工作
- flask - 对象作为 SQL 文字值是不合法的
- c++ - cpp 中的条件运算符是否评估两个操作数?