Call function display to textbox

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
chexmix
Posts: 5
Last visit: Sun Dec 09, 2018 9:39 am

Call function display to textbox

Post by chexmix »

I have a function that I call in various places to display information. It works fine in powershell console but I can't figure out how to display it in a textbox.

I call the function like so.

log -l "Trying to import Active Directory module"
log -l "Unable to import Active Directory module" -type "ERROR"

Here is the function code

Code: Select all

function Log {
    #Create a logging function that writes to console, and colour codes according to whether it's an Error or not
    Param($l, $type)
    $time = get-date -f HH:mm:ss
    if ($type -match "error"){
        write-host $time -f Gray -NoNewline ; write-host " ERROR: " -f Red -NoNewline ; write-host $l -f DarkYellow
    }
    elseif ($type -match "warn"){
        write-host $time -f Gray -NoNewline ; write-host " WARNING: " -f Magenta -NoNewline ; write-host $l -f Yellow
    }
    else {
        write-host $time -f Gray -NoNewline ; write-host " INFO: " -f Cyan -NoNewline ; write-host $l -f Green
    }
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Call function display to textbox

Post by jvierra »

Write-Host cannot be used to display anything in a form. Change all write Host output to the following.

$textbox1.Lines += < your message text >

Here is an example:

Code: Select all

function Log {
    Param($l, $type = 'INFO')

     $msg = '[{0:HH:MM:SS}][{1}]{2}' -f [datetime]::Now,$type,$l
    $textbox1.Lines += $msg
    Write-Host $msg # for verification when running in PowerShell Studio
}
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked