Wakan Tanka
Wakan Tanka

Reputation: 8052

custom event handler which will change Form.Label.Text

I'm able to create simple form which displays some text and do some action after button press. Here is my code I'm playing with:

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 
}

But now I need something more complicated. Let's say I would like to display info on the Form.Label about the status of firefox. I can check whether firefox is running or not

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

but how to show the results of Get-FirefoxStatus function inside Form.Label do I need separate thread that will periodically call Get-FirefoxStatus function? Or is there some type of handler which I can register? Or some event loop? Do I need some kind of refresh button? or what to do, is this even possible in powershell?

Upvotes: 0

Views: 682

Answers (1)

Theo
Theo

Reputation: 61178

As promised, here the code you could use to change the label text in the form using a Timer.

Note that I changed the function to get the running status of Firefox to Test-Firefox so now it returns either $true or $false.

I left the Button_Click function unchanged.

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

Upvotes: 1

Related Questions