Author Posts

January 1, 2012 at 12:00 am

by dcasper at 2012-11-16 07:02:39

Hello all!

I'm trying to figure out how to write information (write-warning, write-output, etc...) to a text box under my form inputs. I would like to use it to show important information when it processes the information thats inputted after the ok button is clicked.

(person inputs into text box, hits OK button, function runs and processes information, and during which outputs information to text box)

Below is snippets of my script, just to show what i have. What i need is how to output to a text box, and have it go line after line retaining the information. When new information is entered, it blanks out and starts over with the new set of inputs.

Example:

#### Process and report

function Process_and_report
{
Do stuff here
REPORT INFORMATION (THIS IS WHAT I NEED HELP WITH)
}

#### Load form

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

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Account Termination"
$objForm.Size = New-Object System.Drawing.Size(600,400)
$objForm.StartPosition = "CenterScreen"

#### Add Button

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,330)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({Process_and_Report})
$objForm.Controls.Add($OKButton)

### Add Input text box

$objLabelInitials = New-Object System.Windows.Forms.Label
$objLabelInitials.Location = New-Object System.Drawing.Size(10,40)
$objLabelInitials.Size = New-Object System.Drawing.Size(60,20)
$objLabelInitials.Text = "Initials:"
$objForm.Controls.Add($objLabelInitials)

$objTextBoxInitials = New-Object System.Windows.Forms.TextBox
$objTextBoxInitials.Location = New-Object System.Drawing.Size(80,37)
$objTextBoxInitials.Size = New-Object System.Drawing.Size(60,20)
$objForm.Controls.Add($objTextBoxInitials)

####

$objForm.Topmost = $True

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

by DonJ at 2012-11-16 09:59:06

(As an aside, try to remember to wrap code in either the CODE or POWERSHELL tags – there are buttons on the editor toolbar for this – really helps make the code readable)

Warnings and errors are written to separate pipelines in PowerShell. Short of writing a custom hosting app, I'm not sure there's a solid way to capture Warnings. Errors, you can trap for using a Try...Catch block and the built-in $error collection (or -ErrorVariable common parameter).