将更改的自定义事件处理程序 Form.Label.Text

custom event handler which will change Form.Label.Text

我能够创建显示一些文本并在按下按钮后执行一些操作的简单表单。这是我正在玩的代码:

Function Button_Click()
{
    [System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
    # Parent.Controls["Label1"].Text = "goodbye, world."
}

Function Generate-Form {
    Add-Type -AssemblyName System.Windows.Forms    
    Add-Type -AssemblyName System.Drawing

    # Build font object
    $Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Italic)


    # Build Button object
    $Button = New-Object System.Windows.Forms.Button
        $Button.Location = New-Object System.Drawing.Size(35,35)
        $Button.Size = New-Object System.Drawing.Size(120,23)
        $Button.Text = "Show Dialog Box"

        #Add Button event 
        $Button.Add_Click({Button_Click})
        # $Button.Add_Click($Button_Click)

    # # Build Label object
    $Label = New-Object System.Windows.Forms.Label
        $Label.Text = "Firefox status"
        $Label.Name = "ffStatus"
        $Label.AutoSize = $True


    # Build Form object
    $Form = New-Object System.Windows.Forms.Form
        $Form.Text = "My Form"
        $Form.Size = New-Object System.Drawing.Size(200,200)
        $Form.StartPosition = "CenterScreen"
        $Form.Topmost = $True
        # $Form.Font = $Font

        # Add button to form
        $Form.Controls.Add($Button)

        # Add label to form
        $Form.Controls.Add($Label)

        #Show the Form 
        $form.ShowDialog()| Out-Null 
}

但现在我需要更复杂的东西。假设我想在 Form.Label 上显示有关 firefox 状态的信息。我可以检查 firefox 是 运行 还是

function Get-FirefoxStatus {
    $ffRunning = 0
    Get-Process| ForEach-Object { if ($_.Name -eq "firefox"){
        $ffRunning = 1}
    }
    Return $ffRunning
}

但是如何在 Form.Label 中显示 Get-FirefoxStatus 函数的结果我需要单独的线程来定期调用 Get-FirefoxStatus 函数吗?或者我可以注册某种类型的处理程序吗?或者一些事件循环?我需要某种刷新按钮吗?或者怎么办,这在 powershell 中是否可行?

正如所承诺的,这里的代码可用于使用 Timer.

更改表单中的标签文本

请注意,我将获取 Firefox 运行 状态的功能更改为 Test-Firefox,所以现在 returns $true$false

我没有更改 Button_Click 函数。

function Button_Click() {
    [System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
}

function Test-Firefox {
    [bool](Get-Process -Name 'firefox' -ErrorAction SilentlyContinue)
}

function Generate-Form {
    Add-Type -AssemblyName System.Windows.Forms    
    Add-Type -AssemblyName System.Drawing

    # create a timer object and set the interval in milliseconds
    $timer = New-Object System.Windows.Forms.Timer
    $timer.Interval = 1000
    # create the Tick event where the text in the label is changed
    $timer.Add_Tick({
        $Label.Text = if (Test-Firefox) { "Firefox is running" } else { "Firefox is not running" }
    })

    # Build font object
    $Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Italic)

    # Build Button object
    $Button = New-Object System.Windows.Forms.Button
        $Button.Location = New-Object System.Drawing.Size(35,35)
        $Button.Size = New-Object System.Drawing.Size(120,23)
        $Button.Text = "Show Dialog Box"

        #Add Button event 
        $Button.Add_Click({Button_Click})

    # # Build Label object
    $Label = New-Object System.Windows.Forms.Label
        $Label.Text = if (Test-Firefox) { "Firefox is running" } else { "Firefox is not running" }
        $Label.Name = "ffStatus"
        $Label.AutoSize = $True


    # Build Form object
    $Form = New-Object System.Windows.Forms.Form
        $Form.Text = "My Form"
        $Form.Size = New-Object System.Drawing.Size(200,200)
        $Form.StartPosition = "CenterScreen"
        $Form.Topmost = $True
        # $Form.Font = $Font

    # Add button to form
    $Form.Controls.Add($Button)

    # Add label to form
    $Form.Controls.Add($Label)

    # Stop and dispose of the timer when the form closes
    $Form.Add_Closing({ $timer.Dispose() })  # Dispose() also stops the timer.

    # Start the timer and Show the Form
    $timer.Start()
    $Form.ShowDialog()| Out-Null

    # don't forget to dispose of the form when done !
    $Form.Dispose()
}

# show the form
Generate-Form