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.
Locked
User avatar
purchasing@admenergy.com
Posts: 2
Joined: Fri Feb 12, 2016 4:20 pm

Output to Multiple RichTextBoxes

Post by purchasing@admenergy.com » Thu Apr 25, 2019 9:20 am

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
Site Admin
Posts: 373
Joined: Sun Mar 03, 2013 12:42 pm

Re: Output to Multiple RichTextBoxes

Post by mxtrinidad » Thu Apr 25, 2019 10:56 am

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
Joined: Thu Aug 18, 2011 4:56 am

Re: Output to Multiple RichTextBoxes

Post by davidc » Thu Apr 25, 2019 11:11 am

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.

jvierra
Posts: 14019
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Output to Multiple RichTextBoxes

Post by jvierra » Thu Apr 25, 2019 2:15 pm

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
purchasing@admenergy.com
Posts: 2
Joined: Fri Feb 12, 2016 4:20 pm

Re: Output to Multiple RichTextBoxes

Post by purchasing@admenergy.com » Fri Apr 26, 2019 11:43 am

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: 14019
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Output to Multiple RichTextBoxes

Post by jvierra » Fri Apr 26, 2019 12:27 pm

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
    )
}

Locked