Page 1 of 1

Output to Multiple RichTextBoxes

Posted: Thu Apr 25, 2019 9:20 am
by purchasing@admenergy.com
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?

Re: Output to Multiple RichTextBoxes

Posted: Thu Apr 25, 2019 10:56 am
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

Re: Output to Multiple RichTextBoxes

Posted: Thu Apr 25, 2019 11:11 am
by davidc
[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]

Re: Output to Multiple RichTextBoxes

Posted: Thu Apr 25, 2019 2:15 pm
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

Re: Output to Multiple RichTextBoxes

Posted: Fri Apr 26, 2019 11:43 am
by purchasing@admenergy.com
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.

Re: Output to Multiple RichTextBoxes

Posted: Fri Apr 26, 2019 12:27 pm
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
    )
}