是否可以 运行 在 Powershell 中单击按钮内的循环?

Is it possible to run a loop within a ButtonClick in Powershell?

我正在尝试使用 Button ClickEvent,但在尝试实现 do-until 循环时卡住了。

在我看来,代码一到 Do 就停止了。

Add-Type -assembly System.Windows.Forms

Clear-Host

$Script:i = 0

# MainForm

$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Size = New-Object System.Drawing.Size (600,600)
$MainForm.Text ='Test'
$MainForm.StartPosition = "CenterScreen"
$MainForm.AutoSize = $true
$MainForm.BringToFront()
$MainForm.BackColor = 'Gray'


# Vocabulary

$Label = New-Object System.Windows.Forms.label
$Label.Location = New-Object System.Drawing.Size (150,200)
$Label.Size = New-Object System.Drawing.Size (250,200)
$Label.Font = New-Object System.Drawing.Font ("Times New Roman",16, 
[System.Drawing.FontStyle]::Bold)
$Label.BackColor = 'Transparent'
$Label.ForeColor = "Blue"
$Label.Text = 'This is some text'
$MainForm.Controls.Add($Label)


$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size (200,400)
$Button.Size = New-Object System.Drawing.Size (200,75)
$Button.Font = New-Object System.Drawing.Font ("Arial",10, 
[System.Drawing.FontStyle]::Bold)
$Button.Text = 'Test'
$MainForm.Controls.Add($Button)
$ButtonClickEvent = {

    Do {
       $Script:i++
       $Label.Text = $Script:i
       }
    Until ($Script:i = 10)    


}

$Button.Add_Click($ButtonClickEvent)


$MainForm.ShowDialog()
$MainForm.Dispose()

有没有办法,我可以在这种方式中包含一个循环,这样当我点击一次按钮时,它就会贯穿整个循环?

如果我删除循环并将其保留为:

       $Script:i++
       $Label.Text = $Script:i

我可以点击并开始计数并运行,但我想在 Click 中有一个循环 运行 用于另一个目的。

在 Powershell 中,赋值使用 =,比较使用 -eq

更改此行:

until ($Script:i = 10)

为此:

until ($Script:i -eq 10)