嗨,我正在尝试在 pwoershell 中创建一个动态表单,这是一个有 5 个按钮(颜色名称)的表单,每个按钮打开一个不同的文本文件(例如,如果单击红色按钮,它应该打开 red.txt;这里是完整代码;
脚本开始
$var = "Red","Blue","Yellow","Black","White"
$testForm = New-Object System.Windows.Forms.Form
$testForm.Text = "Color List"
$testForm.AutoSize = $True
$testForm.AutoSizeMode = "GrowAndShrink"
$Font = New-Object System.Drawing.Font("Times New Roman",24, [System.Drawing.FontStyle]::Bold)
$testForm.Font = $Font
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Select the Color"
$Label.AutoSize = $True
$testForm.Controls.Add($Label)
$x=100
$y=50
foreach($color in $var)
{
$run = New-Object System.Windows.Forms.Button
$run.Location = New-Object System.Drawing.Size($x,$y)
$run.Size = New-Object System.Drawing.Size(100,50)
$run.Text = "$Color"
$run.Add_Click({ Invoke-Expression "notepad C:\Users\User\$color.txt" })
$testForm.Controls.Add($run)
$Font = New-Object System.Drawing.Font("Times New Roman",14,[System.Drawing.FontStyle]::Regular)
$run.font = $Font
$run.AutoSize = $True
$y+=50
}
$testForm.ShowDialog()
结束脚本
一切都很好,直到当表单打开按钮时,点击所有按钮时,打开文件“White.txt”,因为它是数组中的最后一个元素;有什么方法可以更改脚本以使每个按钮只打开它们各自的文件而不是最后一个颜色文件?
如果需要任何进一步的问题或澄清,请告诉我。