Winforms 在PowerShell表单中,与Until循环、Get Random和鼠标单击相结合,这是一个艰难的过程

Winforms 在PowerShell表单中,与Until循环、Get Random和鼠标单击相结合,这是一个艰难的过程,winforms,powershell,arraylist,until-loop,Winforms,Powershell,Arraylist,Until Loop,我正在与一个直到循环、一个随机选取器和一个PowerShell形式的鼠标点击相结合 我可以在没有表单的情况下运行一个随机选取器,其中我有一个工作日,一个一个地随机选取,直到arraylist为空为止。效果不错 $Weekdays = 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday' [System.Collections.ArrayList]$arraylist = $Weekdays Write-H

我正在与一个直到循环、一个随机选取器和一个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()
现在我只剩下几行代码了,我无法以这种方式包含,所以它按照下面的方式工作

testform打开,在一个标签中,我看到它是由一个工作日随机选择的。单击“下一步”将从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)

我一直在玩代码,并尝试使用
按钮。Add_Click()
按钮clickevent{}
但要么是循环没有运行,要么是计数器工作不正常,要么是我以某种方式把代码弄乱了,卡在某个地方,甚至表单也没有显示。

下面对脚本进行的增强调整在表单中实现了某种循环

请注意,没有循环关键字(如
do
while
until
),如果使用了
关键字,甚至没有

### 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()
###加载用于创建表单和控件的程序集###
if(-not(“System.Windows.Forms.Form”-作为[type])){
添加类型-AssemblyName System.Windows.Forms
}
if(-not(“System.Drawing.Font”-作为[type])){
添加类型-AssemblyName System.Drawing
}
$TestForm=New Object System.Windows.Forms.Form
$TestForm.Size=新对象System.Drawing.Size(1200800)
$TestForm.Text='Random Test'
$TestForm.StartPosition=“中心屏幕”
$TestForm.AutoSize=$true
$TestForm.BringToFront()
$TestForm.BackgroundImageLayout=“拉伸”
$TestLabel=新对象System.Windows.Forms.label
$TestLabel.Location=新对象System.Drawing.Size“500200”
$TestLabel.Size=新对象System.Drawing.Size“600,60”
$TestLabel.Font=New Object System.Drawing.Font('Times New Roman','20',[System.Drawing.FontStyle]::粗体)
$TestLabel.BackColor='Transparent'
$TestLabel.ForeColor=“蓝色”
$TestForm.Controls.Add($TestLabel)
$TestLabe2=新对象System.Windows.Forms.Label
$TestLabe2.Location=新对象System.Drawing.Size“200300”
$TestLabe2.Size=新对象System.Drawing.Size“900200”
$TestLabe2.Font=新对象System.Drawing.Font([System.Windows.Forms.Label]::DefaultFont.Name,'16',[System.Drawing.FontStyle]::斜体)
$TestLabe2.BackColor='Transparent'
$TestLabe2.ForeColor=[System.Drawing.Color]::午夜蓝色
$TestForm.Controls.Add($TestLabe2)
$TestButton=新建对象System.Windows.Forms.Button
$TestButton.Location=新对象System.Drawing.Size“500600”
$TestButton.Size=新对象System.Drawing.Size“200,75”
$TestButton.Font=新对象System.Drawing.Font('Arial','10',[System.Drawing.FontStyle]::粗体)
$TestButton.Text='Next Random'
$TestForm.Controls.Add($TestButton)
$TestButtoX=新对象System.Windows.Forms.Button
$TestButtoX.Location=新对象System.Drawing.Size“200600”
$TestButtoX.Size=新对象System.Drawing.Size“200,75”
$TestButtoX.Font=新对象System.Drawing.Font('Arial','10',[System.Drawing.FontStyle]::粗体)
$TestButtoX.Text='Next Round'
$TestButtoX.Enabled=$false
$TestForm.Controls.Add($TestButtoX)
功能交换按钮{
$TestButton.Enabled=[bool]$script:Weekdays.Count
$TestButtoX.Enabled=-not[bool]$script:Weekdays.Count
}
函数移除工作日{
$script:removetask=Get Random$script:Weekdays.ToArray()
$script:Weekdays.Remove($script:removetask)
$TestLabe2.Text=('(保留{0})-f$script:Weekdays.Count),($script:Weekdays-join',')-join':'
$TestLabel.Text=$script:removetask
交换按钮
}
函数定义视图{
$script:Weekdays=[System.Collections.ArrayList]([System.Enum]::GetNames([System.DayOfWeek]))

在窗体中运行循环总是有问题的。你能不能在单击事件中完成所有操作?每次单击鼠标时测试数组是否为空?这是真的,可能会很棘手。但我认为,有可能使用if条件来代替,这样,我可能能够克服这个问题,并将其构建到单击事件中。我一定会这样试的。谢谢你的尝试