locked
Unable to get inputbox value RRS feed

  • Question

  • I thought this would be easy enough but I can't see what I am doing wrong.  When I click the button I'd like to get the value entered in the textbox, unfortunately it does not work.  Please tell me what I am missing.  In the code below I am using the variable to set the count of Get-Random.  However even doing a write-host xxx.Text writes null.

        $sacraficeBox = New-Object System.Windows.Forms.TextBox
        $sacraficeBox.Location = New-Object System.Drawing.Size(250,100) 
        $sacraficeBox.Size = New-Object System.Drawing.Size(40,30) 
        $sacraficeBox.MaxLength = 2
        $sacraficeBox.Height = 30
        
        $objForm.Controls.Add($sacraficeBox)
    
        $AppeaseButton = New-Object System.Windows.Forms.Button
        $AppeaseButton.Location = New-Object System.Drawing.Size(75,150)
        $AppeaseButton.Size = New-Object System.Drawing.Size(300,23)
        $AppeaseButton.Text = "Click Me"
        $AppeaseButton.Add_Click({$chosen = $survivors | Get-Random -count $sacraficeBox.Text})
        $objForm.Controls.Add($AppeaseButton)

    The error message I get is:

    Get-Random : Cannot validate argument on parameter 'Count'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not
    contain any null values and then try the command again.

    Friday, November 25, 2016 4:57 PM

Answers

  • Then you have other issues with your form.

    This works on all of my systems:

    Add-Type -AssemblyName System.Windows.Forms
    $form = New-Object System.Windows.Forms.Form
    $sacraficeBox = New-Object System.Windows.Forms.TextBox
    $sacraficeBox.Location = '10,10'
    $sacraficeBox.Size = '40, 30'
    $sacraficeBox.MaxLength = 2
    $sacraficeBox.Text = 3
    $sacraficeBox.Height = 30
    
    $form.Controls.Add($sacraficeBox)
    
    $AppeaseButton = New-Object System.Windows.Forms.Button
    $AppeaseButton.Location = '10, 50'
    $AppeaseButton.Text = "Click Me"
    $AppeaseButton.Add_Click({ Write-Host ( @(1,2,3,4,5) | Get-Random -count $sacraficeBox.Text) -fore green })
    $form.Controls.Add($AppeaseButton)
    $form.ShowDialog()


    \_(ツ)_/

    • Marked as answer by Daniel Kaliel Friday, November 25, 2016 9:50 PM
    Friday, November 25, 2016 9:09 PM

All replies


  •     $AppeaseButton
    .Add_Click({$chosen =$survivors | Get-Random -count [int]($sacraficeBox.Text)})

    \_(ツ)_/

    Friday, November 25, 2016 5:08 PM
  • Tried that and I get the error:

    Cannot bind parameter 'Count'. Cannot convert value "[int]" to type "System.Int32". Error: "Input string was not in a correct format."

    If you remove [int] then I get the null error above.  I am typing in the number 3 in the text box before clicking the button.
    Friday, November 25, 2016 8:58 PM
  • Then you have other issues with your form.

    This works on all of my systems:

    Add-Type -AssemblyName System.Windows.Forms
    $form = New-Object System.Windows.Forms.Form
    $sacraficeBox = New-Object System.Windows.Forms.TextBox
    $sacraficeBox.Location = '10,10'
    $sacraficeBox.Size = '40, 30'
    $sacraficeBox.MaxLength = 2
    $sacraficeBox.Text = 3
    $sacraficeBox.Height = 30
    
    $form.Controls.Add($sacraficeBox)
    
    $AppeaseButton = New-Object System.Windows.Forms.Button
    $AppeaseButton.Location = '10, 50'
    $AppeaseButton.Text = "Click Me"
    $AppeaseButton.Add_Click({ Write-Host ( @(1,2,3,4,5) | Get-Random -count $sacraficeBox.Text) -fore green })
    $form.Controls.Add($AppeaseButton)
    $form.ShowDialog()


    \_(ツ)_/

    • Marked as answer by Daniel Kaliel Friday, November 25, 2016 9:50 PM
    Friday, November 25, 2016 9:09 PM
  • You are correct.  I had the variables scoped wrong.
    Friday, November 25, 2016 9:50 PM