我正在努力解决直到循环与随机选择器和 PowerShell 表单中的鼠标单击相结合的问题。
我可以在没有表单的情况下运行随机选择器,在该表单中,我有工作日,随机选择,并且一天一天地被删除,直到 arraylist 为空。工作还不错。
$Weekdays = 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'
[System.Collections.ArrayList]$arraylist = $Weekdays
Write-Host $arraylist -ForegroundColor Green
pause
do {
$removetask = Get-Random $arraylist.ToArray()
$arraylist.Remove($removetask)
Write-Host $removetask
Write-Host $arraylist -ForegroundColor Red
pause
} until ($arraylist.Count -eq 0)
在另一种方法中,我尝试做同样的事情,但是这一次,我想控制循环本身,只要从 arraylist 中获取第一个键并显示在标签中,我就必须单击鼠标按钮,所以它继续随机取下一个。
没有do {} until ()
我到目前为止:
$TestForm = New-Object System.Windows.Forms.Form
$TestForm.Size = New-Object System.Drawing.Size (1200,800)
$TestForm.Text ='Random Test'
$TestForm.StartPosition = "CenterScreen"
$TestForm.AutoSize = $true
$TestForm.BringToFront()
$TestForm.BackgroundImageLayout = "Stretch"
[System.Collections.ArrayList]$Weekdays = 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'
$TestLabel = New-Object System.Windows.Forms.label
$TestLabel.Location = New-Object System.Drawing.Size '500,200'
$TestLabel.Size = New-Object System.Drawing.Size '600,60'
$TestLabel.Font = New-Object System.Drawing.Font ('Times New Roman','20',[System.Drawing.FontStyle]::Bold)
$TestLabel.BackColor = 'Transparent'
$TestLabel.ForeColor = "Blue"
$removetask = Get-Random $Weekdays.ToArray()
$TestLabel.Text = $removetask
$TestForm.Controls.Add($TestLabel)
$TestButton = New-Object System.Windows.Forms.Button
$TestButton.Location = New-Object System.Drawing.Size '500,600'
$TestButton.Size = New-Object System.Drawing.Size '200,75'
$TestButton.Font = New-Object System.Drawing.Font ('Arial','10',[System.Drawing.FontStyle]::Bold)
$TestButton.Text = 'Next Random'
$TestForm.Controls.Add($TestButton)
$TestButton.Add_Click()
$TestForm.ShowDialog()
$TestForm.Dispose()
现在我还剩下几行代码,我无法以这种方式包含,所以它按以下方式工作。
测试表打开,在我看到的一个标签中随机选择一个工作日。单击下一步将从 arraylist 中删除所选的工作日并随机显示下一个工作日,并将一直持续到 arraylist 为空。
拼图缺少的部分是:
### The loop itself
do {} until ()
### code to find a Random value from $weekdays and write it into $removetask
$removetask = Get-Random $Weekdays.ToArray()
### code to remove the randomly chosen day and remove it from the arraylist
$Weekdays.Remove($removetask)
#### check if array is empty
($weekdays.Count -eq 0)
我正在玩代码并尝试使用Button.Add_Click()
,ButtonClickEvent {}
但要么循环没有运行,计数器工作不正常,要么我以某种方式弄乱了代码,它卡在某个地方,甚至没有表格正在显示。