Output to Multiple RichTextBoxes

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 4 years and 11 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
User avatar
purchasingADM
Posts: 2
Last visit: Mon Jan 20, 2020 4:59 pm

Output to Multiple RichTextBoxes

Post by purchasingADM »

PowerShell Studio 2017 Version 5.4.136 64 bit
Running on Windows 10 64 bit

Hopefully I can explain this simply

I am building a tool for our general IT usage. One of the ideas I am perusing is having a richtextbox that reports information. When you select a user from a combobox, it runs the function to write a report on the user and it prints in a report box.
That part is easy.
If you make changes to the user, I wanted a second richtextbox to get a report that showed the same report, but with the changes made, that way the original report and the updated report are next to each other in separate richtextboxes.

In the Function Write-Report, I pass the user information in, and then things like this happen …

## Selected AD Name
$rtbReport.SelectionFont = $ReportFonts.SubTitleFont
$rtbReport.SelectionColor = $ReportFonts.SubTitleColor
$rtbReport.AppendText($SelectedUser.Name)
$rtbReport.AppendText("`n")

I create different font styles for different lines, and then put the name in the box.

So here is the problem.
I don’t want to have two reports, one with $rtbReport being written too, and another that writes the same stuff to $rtbReportUpdate.

I can't seem to figure out a way to create the function Write-Report, so that I can choose which richtextbox to use.

Theoretically it would be something like Write-Report -ADSelectedUser $selecteduser -Report rtbReport (or rtbReportUpdate)

Perhaps there is a better way to do this, or I am just spinning my wheels thinking there is a way to do this

Anyone with a suggestion?
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Output to Multiple RichTextBoxes

Post by mxtrinidad »

You will need to build the logic to compare original vs updated line by line in order to identify the difference.

A good start would be to use the Compare-Object that can identify difference. You will need to be creative and create the PSObject to compare against line by line.

Use the following command and look at the examples:

Get-Help Compare-Object -ShowWindow

Or,

Get-Help Compare-Object -Examples
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Output to Multiple RichTextBoxes

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Output to Multiple RichTextBoxes

Post by jvierra »

Just pass the control to the function.

Code: Select all

function Write-Report{
    Param(
        $RtbControl
    )
    $RtbControl.SelectionFont = $ReportFonts.SubTitleFont
    $RtbControl.SelectionColor = $ReportFonts.SubTitleColor
    $RtbControl.AppendText($SelectedUser.Name)
    $RtbControl.AppendText("`n")
}
Write-Report -RtbControl $rtbReport1 
Write-Report -RtbControl $rtbReport2
User avatar
purchasingADM
Posts: 2
Last visit: Mon Jan 20, 2020 4:59 pm

Re: Output to Multiple RichTextBoxes

Post by purchasingADM »

The first thing I tried was passing it into the function as a parameter, but it didn't work. I tried it again today on a smaller scale test form and it did work.
I think what I did the first time was just copy a template parameter that I keep for using over and over. The template parameter though has [string]$variable, and I didn't notice it. That's probably why it failed. Thanks. I need to use this board more often.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Output to Multiple RichTextBoxes

Post by jvierra »

The default parameter is [object]. Any argument passed to [object] will not be converted. Any other type will cause an attempt to convert to that type. Using [string] will convert the argument to a string if possible.

If you want to type check a control then use the full base type or the actual control type.

[System.Windows.Forms.Control] or [System.Windows.Forms.RichTextBox]

The default or [object] will work. Internal utility functions do not have to be strongly typed. In an external function library then we may want to do specific type declaration.

Code: Select all

function DoSomething{
    param (
        [System.Windows.Forms.RichTextBox]$rtb
    )
}
This topic is 4 years and 11 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