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

时间:2019-12-10 01:03:10

标签: multithreading winforms powershell events event-loop

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

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
}

但是如何在Get-FirefoxStatus内显示Form.Label函数的结果,我是否需要一个单独的线程来周期性地调用Get-FirefoxStatus函数?还是我可以注册某种处理程序?还是一些事件循环?我需要某种刷新按钮吗?或该怎么做,甚至在powershell中也是可能的吗?

1 个答案:

答案 0 :(得分:1)

如所承诺的那样,这里的代码可用于使用Timer更改表单中的标签文本。

请注意,我将功能更改为将Firefox的运行状态更改为Test-Firefox,因此现在它返回$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