Output to RichTextBox Font Color Changing Colors ( Not Desired)

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 9 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
shiroscout
Posts: 41
Last visit: Sat Jul 15, 2023 7:13 pm

Output to RichTextBox Font Color Changing Colors ( Not Desired)

Post by shiroscout »

Hello,

I am using an output into a rich text box.

I had my buttons output working with single action button output into textbox.
All my buttons are mostly just local pc information gathering.
My button codes almost all look like this:

Code: Select all

$buttonInstalledLocalPrinte_Click={
    #TODO: Place custom script here

	$richtextbox1.Text =
    Get-Printer -ComputerName $Env:Computername | Out-String
  
}
Then I changed them to this:

Code: Select all

$buttonInstalledLocalPrinte_Click={
    #TODO: Place custom script here
    $richtextbox1.SelectAll();
    $richtextbox1.Copy();
	$richtextbox1.Text =
    Get-Printer -ComputerName $Env:Computername | Out-String
    $richtextbox1.Paste()
}
I am trying to get many of the buttons to run and "Append my previous output and add my new button function output.

I am not a coder so please excuse the bade code, but, it does add the previous output and the new output.
Problem: Sometimes it adds red text and sometimes white text. It depends, or so it seems to depend on what button I am clicking on and what order.

It there a way someone can lead me in the right direction?
Honestly, it would work if I could just force Black Text on every output return to the rich text box.
Thank You,

Wayne
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 RichTextBox Font Color Changing Colors ( Not Desired)

Post by jvierra »

Just add the new text to the control. There is no need to copy and paste.

$RichTextBox.AppendText($text)

Nothing in your code changes any colors. Have you changed the colors somewhere else in the code?
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 RichTextBox Font Color Changing Colors ( Not Desired)

Post by jvierra »

You can also do this:

$richtextbox.Lines += Get-Printer | Out-String

There is no need to specify the computer name with the local system.
User avatar
shiroscout
Posts: 41
Last visit: Sat Jul 15, 2023 7:13 pm

Re: Output to RichTextBox Font Color Changing Colors ( Not Desired)

Post by shiroscout »

Hello,

Thank you for the fast response.
I will look but do not know if there is anything referencing and font colors in other code. I have only been going off your support info center and tutorials.
I would have to look and see if one of toolbox objects when dragged to page references for color changes in it's code.
But, I think it has been resolved by using your line or code, as I stopped seeing issues once I changed to that line of code.

It is not imperative that each button Amends Text, but it would make it easier for the user.
I'm using your code you suggested:

Code: Select all

$RichTextBox.AppendText($text)
and it works, but not always.
When it does not work I get no output at all, so I reverted that button to just have the :

Code: Select all

$RichTextBox.Text
on the buttons that code was not working on.
If possible, I would like it to work on this button, but this uses the Get-ComputerInfo command and the thing that stands out about this problem is that it pops up several services or applets while gathering the info and I am not sure if that has something to do with it failing.

Code: Select all

$buttonCollectPCInformation_Click={
    #TODO: Place custom script here
    $richtextbox1.Text =
    Get-ComputerInfo -Property "CsName",
                     "BiosSeralNumber",
                     "CsUserName",
                     "LogonServer",
                     "WindowsProductName",
                     "OsArchitecture",
                     "WindowsVersion",
                     "CsManufacturer",
                     "CsModel",
                     "CsChassisSKUNumber",
                     "BiosSMBIOSBIOSVersion",
                     "BiosFirmwareType",
                     "OsSystemDrive",
                     "OsWindowsDirectory",
                     "OsSystemDirectory",
                     "KeyboardLayout",
                     "CsPhyicallyInstalledMemory",
                     "OsBootDevice",
                     "OsSystemDevice",
                     "TimeZone" | Format-Custom | Out-String -Width 300
                    # "OsInstallDate" 
                    # "OsLocalDateTime",
                    # "OsLastBootUpTime", 
                    # "OsUptime" 



	
}
Thank You,

Wayne
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 RichTextBox Font Color Changing Colors ( Not Desired)

Post by jvierra »

I haven't tested this but "pasting" into an RTB control may display ina highlight color.

Since there is no reason to use copy/pasted then you are set.

You need to read all of the documentation for each control you try to use. Using components has nothing to do with "coding" experience. It has to do with learning the tech ology you wih to work with.

Anyone can learn coding in few hours. Children learn coding in first grade or earlier. Here is a site that will teach coding in a couple of hours and is free. It has course for any age from 5 to 555. Pick your level and begin. I like the K-5 level because it is the most entertaining level and I have been writing code since 1961.

https://studio.code.org/courses

Tou can also start with the intro video which takes only a couple of minutes: https://www.youtube.com/watch?v=nKIu9yen5nc
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 RichTextBox Font Color Changing Colors ( Not Desired)

Post by jvierra »

You use of "Format-Custom" does not give what you think. It produces "class" code:

Code: Select all

class PSCustomObject
{
  CsName = ALPHA
  BiosSeralNumber = CND61408V7
  CsUserName = ALPHA\jvierra
  LogonServer = \\ALPHA
  WindowsProductName = Windows 10 Pro
  OsArchitecture = 64-bit
  WindowsVersion = 1809
  CsManufacturer = HP
  CsModel = HP ENVY Notebook
  CsChassisSKUNumber =
  BiosSMBIOSBIOSVersion = F.35
  BiosFirmwareType = Uefi
  OsSystemDrive = C:
  OsWindowsDirectory = C:\WINDOWS
  OsSystemDirectory = C:\WINDOWS\system32
  KeyboardLayout = en-US
  CsPhyicallyInstalledMemory = 16777216
  OsBootDevice = \Device\HarddiskVolume1
  OsSystemDevice = \Device\HarddiskVolume3
  TimeZone = (UTC-05:00) Eastern Time (US & Canada)
}
The following gives you the results:

Code: Select all

$buttonCollectPCInformation_Click = {
    $properties = @(
        'CsName',
        'BiosSeralNumber',
        'CsUserName',
        'LogonServer',
        'WindowsProductName',
        'OsArchitecture',
        'WindowsVersion',
        'CsManufacturer',
        'CsModel',
        'CsChassisSKUNumber',
        'BiosSMBIOSBIOSVersion',
        'BiosFirmwareType',
        'OsSystemDrive',
        'OsWindowsDirectory',
        'OsSystemDirectory',
        'KeyboardLayout',
        'CsPhyicallyInstalledMemory',
        'OsBootDevice',
        'OsSystemDevice',
        'TimeZone'
    )
    $text = Get-ComputerInfo -Property $properties | Format-List | Out-String
    $richtextbox1.AppendText($text)
}
User avatar
shiroscout
Posts: 41
Last visit: Sat Jul 15, 2023 7:13 pm

Re: Output to RichTextBox Font Color Changing Colors ( Not Desired)

Post by shiroscout »

Hello,

I just saw this message.
Thank you.
That cleaned the text up a bit and beautified it a bit more.
I'm a noob, and knew that it was not getting formatted like I needed, but I am still trying to get a grasp on basic Windows Forms fundamental information.
I'm trying to get an understanding of the $ and it's use, as well as trying to understand the Windows.XXX .

Thank you again for adding value for me by responding with things you noticed about my current ghetto code.
Thank You,

Wayne
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 RichTextBox Font Color Changing Colors ( Not Desired)

Post by jvierra »

We have no idea what Windows.xxx is. It is not something we use in any Windows language or system.

You cannot work with forms until you learn basic PowerShell. Learning PowerShell will teach you about "$".

Microsoft Virtual Academy - Getting Started with Microsoft PowerShell

PowerShell Documentation
PowerShell Style Guidelines
User avatar
shiroscout
Posts: 41
Last visit: Sat Jul 15, 2023 7:13 pm

Re: Output to RichTextBox Font Color Changing Colors ( Not Desired)

Post by shiroscout »

Hello,

Thank you for the links and references. I'll be studying the information you provided.
Thank You,

Wayne
This topic is 4 years and 9 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