locked
Text box Change RRS feed

  • Question

  • Hello

    I have a textbox on a Powershell form (blank) and I have a button.

    When I change the textbox value (type in a computername), I click the "reboot" button.  It should take the value from the textbox and use it for the name of the computer to restart (restart-computer $txthostname.text).

    Here's my code:

    $txtHostname = New-Object System.Windows.Forms.TextBox
    $txtHostname.width = 250
    $txtHostname.location = New-Object system.drawing.point(15,35)
    $txtHostname.Add_TextChanged({$Comp = $txtHostname.Text
                                })
    
    
    $btnReboot = New-Object System.Windows.Forms.Button
    $btnReboot.location = New-Object system.drawing.size(15,55)
    $btnReboot.size = New-Object system.drawing.size(75,25)
    $btnReboot.Text= "Reboot"
    $btnReboot.Add_Click({If($txtHostname.Text.Length -gt 0) {rebootcomputer}
    Else{[System.Windows.MessageBox]::Show($Comp)}
                            })
    $RebootCom.controls.Add($btnReboot)
    
    Function Rebootcomputer() {
    If($txtHostname.Text.length -gt 0) {
    Restart-Computer $txtHostname.Text}
    }

    I've been trying lots of different ways to do this (and ended up rebooting my own system several times), but I can't seem to be able to grab the text from the textbox.  I created the system box so I can see what value is being picked up and it's always blank and the computer never reboots.

    Any Suggestions?

    Thank You

    Terry

    Thursday, March 7, 2019 7:50 PM

Answers

  • This works perfectly:

    Add-Type -AssemblyName System.Windows.Forms
    $RebootCom = New-Object System.Windows.Forms.Form
    $RebootCom.StartPosition = 'CenterScreen'
    
    $txtHostname = New-Object System.Windows.Forms.TextBox
    $RebootCom.Controls.Add($txtHostname)
    $txtHostname.width = 250
    $txtHostname.location = '15, 35'
    
    $btnReboot = New-Object System.Windows.Forms.Button
    $RebootCom.Controls.Add($btnReboot)
    $btnReboot.location = '15, 55'
    $btnReboot.size = '75, 25'
    $btnReboot.Text = "Reboot"
    $btnReboot.Add_Click({
        $btnReboot.Enabled = $false
        if ($txtHostname.Text.Length) {
            Restart-Computer $txtHostname.Text -Verbose
        }
        $btnReboot.Enabled = $true
    })
    $RebootCom.ShowDialog()
    


    \_(ツ)_/

    • Marked as answer by terryvanduzee Thursday, March 7, 2019 11:35 PM
    Thursday, March 7, 2019 10:51 PM

All replies

  • Do this:

    $btnReboot.Add_Click({
        if($txtHostname.Text.Trim()){
            Restart-Computer $txtHostname.Text
        }
    })
    
    


    \_(ツ)_/

    Thursday, March 7, 2019 8:57 PM
  • Thank You for your reply.

    I get an error when running it.  Seems the script is not picking up the value from the textbox.

    You cannot call a method on a null-valued expression.
    At C:\Users\terry-admin\Documents\Scripts 3\Toolbox.ps1:110 char:8
    +     if($txtHostname.Text.Trim()){
    +        ~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Will I still need the $txtHostname.Add_TextChanged  ?

    Thank you

    Thursday, March 7, 2019 10:42 PM
  • This is on a form that is generated from the main form.
    Thursday, March 7, 2019 10:43 PM
  • Works fine for me.  Perhaps your form is not built correctly.


    \_(ツ)_/

    Thursday, March 7, 2019 10:45 PM
  • This works perfectly:

    Add-Type -AssemblyName System.Windows.Forms
    $RebootCom = New-Object System.Windows.Forms.Form
    $RebootCom.StartPosition = 'CenterScreen'
    
    $txtHostname = New-Object System.Windows.Forms.TextBox
    $RebootCom.Controls.Add($txtHostname)
    $txtHostname.width = 250
    $txtHostname.location = '15, 35'
    
    $btnReboot = New-Object System.Windows.Forms.Button
    $RebootCom.Controls.Add($btnReboot)
    $btnReboot.location = '15, 55'
    $btnReboot.size = '75, 25'
    $btnReboot.Text = "Reboot"
    $btnReboot.Add_Click({
        $btnReboot.Enabled = $false
        if ($txtHostname.Text.Length) {
            Restart-Computer $txtHostname.Text -Verbose
        }
        $btnReboot.Enabled = $true
    })
    $RebootCom.ShowDialog()
    


    \_(ツ)_/

    • Marked as answer by terryvanduzee Thursday, March 7, 2019 11:35 PM
    Thursday, March 7, 2019 10:51 PM
  • Interesting.  This works for me too.

    I'll have to go through the differences and see what is wrong with my coding.

    Thank you so much.

    Terry

    Friday, March 8, 2019 3:25 PM