0

I'm struggling here.

Take this sample code:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(500,400) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$output1=$objTextBox.Text}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(100,90)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "Merge"
$OKButton.Add_Click({$output=$objTextBox.Text})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(300,90)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(160,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please enter the target machine: "
$objForm.Controls.Add($objLabel) 

$output = new-object System.Windows.forms.label
$output.location = new-object System.drawing.size(30, 140)
$output.size = New-Object System.Drawing.Size(300,300) 
$objform.controls.add($output)

$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(115,40) 
$objTextBox.Size = New-Object System.Drawing.Size(260,20) 
$objForm.Controls.Add($objTextBox) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$output

I can't for the life of me figure out why when I enter text into the box and click the OKButton (Merge) nothing happns. It should be outputted to the $output, or at least, thats what I want to happen.

I'm simply trying to get the code to take the input of the text box and output it to the label form called $output.

I've got it to work in the sense that when I put input into the text box and press 'Merge', it outputs it to the Powershell_ISE console, but only after the window is closed and in run for a second time (It's not refreshing the $output label when I press 'merge' and only outputs when I close the program)

Please help!

Thank you

1 Answer 1

2

Modify

$OKButton.Add_Click({$output=$objTextBox.Text})

to

$OKButton.Add_Click({$output.text=$objTextBox.Text})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.