Insert your output into textbox form when button is clicked.

Hi
I am trying to insert my ping result to the textbox when button is clicked. Below is the PS for GUI.

Add-Type -AssemblyName System.Windows.Forms

$Form = New-Object system.Windows.Forms.Form
$Form.Text = "test"
$Form.TopMost = $true
$Form.Width = 1000
$Form.Height = 1000
$Form.FormBorderStyle = "Fixed3D"
$form.StartPosition = "centerScreen"
$form.ShowInTaskbar = $true

$InputBox = New-Object system.windows.Forms.TextBox
$InputBox.Multiline = $true
$InputBox.BackColor = "#A7D4F7"
$InputBox.Width = 280
$InputBox.Height = 400
$InputBox.ScrollBars = "Vertical"
$InputBox.location = new-object system.drawing.point(10, 30)
$InputBox.Font = "Microsoft Sans Serif,10,style=Bold"
$Form.controls.Add($inputbox)

$outputBox = New-Object System.Windows.Forms.RichTextBox
$outputBox.Multiline = $true
$outputBox.BackColor = "#FDFEFE"
$outputBox.Width = 960
$outputBox.Height = 450
$outputBox.ReadOnly = $true
$outputBox.ScrollBars = "Both"
$outputBox.WordWrap = $false
$outputBox.location = new-object system.drawing.point(10, 500)
$outputBox.Font = "Lucida Sans Typewriter,9"
$Form.controls.Add($outputBox)

$Pingbutton = New-Object system.windows.Forms.Button
$Pingbutton.BackColor = "#5bd22c"
$Pingbutton.Text = "Ping"
$Pingbutton.Width = 120
$Pingbutton.Height = 22
$Pingbutton.location = new-object system.drawing.point(309, 30)
$Pingbutton.Font = "Microsoft Sans Serif,8,style=bold"
$Pingbutton.FlatAppearance.MouseOverBackColor = [System.Drawing.Color]::FromArgb(255, 255, 36)
$Pingbutton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$Pingbutton.Cursor = [System.Windows.Forms.Cursors]::Hand
$Pingbutton.Add_Click( { pingInfo })
$Form.controls.Add($Pingbutton)

# show form
[void] $Form.ShowDialog()

And below is the pingInfo function.

function pingInfo {
$outputBox.Clear()
$computers = $InputBox.lines.Split("`n")
foreach ($computer in $computers) {
if (Test-Connection $computer -Count 1 -Quiet) {
Write-Host $computer is Online -ForegroundColor Green
}
else {
Write-Host $computer is Offline -ForegroundColor Red
}
}
}

How can I direct the ping result to $outputBox

Hello mrdon03,

Try something like this:

if (Test-Connection $computer -Count 1 -Quiet) {
   $outputBox.AppendText("$computer is Online`n")
}
else {
    $outputBox.AppendText("$computer is Offline`n")
}

Reference.

Hope that helps.

[quote quote=259579]Hello mrdon03,

Try something like this:

PowerShell
<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick">if (Test-Connection $computer -Count 1 -Quiet) { $outputBox.AppendText("$computer is Online`n") } else { $outputBox.AppendText("$computer is Offline`n") }</textarea>
1
2
3
4
5
6
if (Test-Connection $computer -Count 1 -Quiet) {
$outputBox.AppendText("$computer is Online`n"</span><span class="crayon-sy">)</span></div> <div id="urvanov-syntax-highlighter-5f72799874d13738147175-3" class="crayon-line"><span class="crayon-sy">}</span></div> <div id="urvanov-syntax-highlighter-5f72799874d13738147175-4" class="crayon-line crayon-striped-line"><span class="crayon-st">else</span> <span class="crayon-sy">{</span></div> <div id="urvanov-syntax-highlighter-5f72799874d13738147175-5" class="crayon-line"><span class="crayon-v">$outputBox</span><span class="crayon-sy">.</span><span class="crayon-e">AppendText</span><span class="crayon-sy">(</span><span class="crayon-s">"$computer is Offline`n")
}
Reference.

Hope that helps.

[/quote]

Thanks. Do you know how can i apply the font color as well.
For example:

computer is Online - GREEN
computer is Offline - RED

Try using SelectionControl property.

Here is C# example

Hope that helps.

Thanks. This is resolved

function pingInfo { 
$outputBox.Clear() 
$computers = $InputBox.lines.Split("`n") 
foreach ($computer in $computers) { 
if (Test-Connection $computer -Count 1 -Quiet) {
$outputBox.Font = "Calibri,11,style=Bold"
$outputBox.SelectionColor = "#007F00"
$outputBox.AppendText("$computer is Online`n")
}
else {
$outputBox.Font = "Calibri,11,style=Bold"
$outputBox.SelectionColor = "#FF0000"
$outputBox.AppendText("$computer is Offline`n")
} 
} 
}

[quote quote=259579]Hello mrdon03,

Try something like this:

<link rel=“stylesheet” type=“text/css” href=“https://powershell.org/wp-content/plugins/urvanov-syntax-highlighter/themes/powershell-ise/powershell-ise.css”>
<link rel=“stylesheet” type=“text/css” href=“https://powershell.org/wp-content/plugins/urvanov-syntax-highlighter/fonts/liberation-mono.css”>

PowerShell
<textarea class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;">if (Test-Connection $computer -Count 1 -Quiet) { $outputBox.AppendText("$computer is Online`n") } else { $outputBox.AppendText("$computer is Offline`n") }</textarea>
1
2
3
4
5
6
if (Test-Connection $computer -Count 1 -Quiet) {
$outputBox.AppendText("$computer is Onlinen")
}
else {
$outputBox.AppendText("$computer is Offlinen")
}

Reference.

Hope that helps.[/quote]

thanks for the appendtext control, wasn’t aware of that. much easier than building large blocks of strings for results.