winforms - 在 PowerShell 表单中与 Get-Random 和鼠标单击相结合的直到循环中挣扎
问题描述
我正在努力解决直到循环与随机选择器和 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 {}
但要么循环没有运行,计数器工作不正常,要么我以某种方式弄乱了代码,它卡在某个地方,甚至没有表格正在显示。
解决方案
您的脚本的以下增强调整在表单中实现了某种循环。
请注意,没有使用循环关键字(如do
, while
, until
),甚至没有if
使用关键字:
### Load Assemblies for creating form & controls ###
if ( -not ("System.Windows.Forms.Form" -as [type]) ) {
Add-Type -AssemblyName System.Windows.Forms
}
if ( -not ("System.Drawing.Font" -as [type]) ) {
Add-Type -AssemblyName System.Drawing
}
$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"
$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"
$TestForm.Controls.Add($TestLabel)
$TestLabe2 = New-Object System.Windows.Forms.Label
$TestLabe2.Location = New-Object System.Drawing.Size '200,300'
$TestLabe2.Size = New-Object System.Drawing.Size '900,200'
$TestLabe2.Font = New-Object System.Drawing.Font ([System.Windows.Forms.Label]::DefaultFont.Name,'16',[System.Drawing.FontStyle]::Italic)
$TestLabe2.BackColor = 'Transparent'
$TestLabe2.ForeColor = [System.Drawing.Color]::MidnightBlue
$TestForm.Controls.Add($TestLabe2)
$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)
$TestButtoX = New-Object System.Windows.Forms.Button
$TestButtoX.Location = New-Object System.Drawing.Size '200,600'
$TestButtoX.Size = New-Object System.Drawing.Size '200,75'
$TestButtoX.Font = New-Object System.Drawing.Font ('Arial','10',[System.Drawing.FontStyle]::Bold)
$TestButtoX.Text = 'Next Round'
$TestButtoX.Enabled = $false
$TestForm.Controls.Add($TestButtoX)
Function Swap-Buttons {
$TestButton.Enabled = [bool]$script:Weekdays.Count
$TestButtoX.Enabled = -not [bool]$script:Weekdays.Count
}
Function RemoveWeekday {
$script:removetask = Get-Random $script:Weekdays.ToArray()
$script:Weekdays.Remove($script:removetask)
$TestLabe2.Text = ('(remain {0})' -f $script:Weekdays.Count), ($script:Weekdays -join ', ') -join ': '
$TestLabel.Text = $script:removetask
Swap-Buttons
}
Function DefineWeek {
$script:Weekdays = [System.Collections.ArrayList]([System.Enum]::GetNames([System.DayOfWeek]))
<#
# debugging: try another array list (a larger one)
$script:Weekdays = [System.Collections.ArrayList]([System.Drawing.Color] |
Get-Member -MemberType Properties -Static -Force |
Where-Object Name -match ".+blue" |
Select-Object -ExpandProperty Name
)
<##>
}
$TestButton.Add_Click({
RemoveWeekday
})
$TestButtoX.Add_Click({
DefineWeek
$TestButtoX.Enabled = $false
$TestButton.Enabled = $true
RemoveWeekday
})
$script:removetask = ''
DefineWeek
RemoveWeekday
$TestForm.ShowDialog()
$TestForm.Dispose()
推荐阅读
- apache - 如何在 SOLR 中搜索包含 [ 和/或 ] 的字段?
- symfony - 如何获取当前登录的角色 symfony?
- r - 如何在一定时间后触发情节悬停?
- python - 无法在 keras 中定义自定义损失函数
- java - 更新后检索数据库列的值
- asp.net - 如何使用敲除和 asp.net 将文件(excel)上传到我的 C 驱动器上的特定位置
- azure - 功能应用程序启动后 TimerTrigger 未触发
- rest - 如何在sails的action2中获取查询参数
- fiware - Orion Admin mashup NGSI 订阅浏览器小部件无法正常工作
- javascript - React 路由器在刷新后渲染组件