Problem getting the current BackColor of a textbox object

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 8 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
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Problem getting the current BackColor of a textbox object

Post by Alexander Riedel »

All UI controls with color settings use the type [System.Drawing.Color].
I use a label here, but it doesn't really matter.

if($label1.BackColor.IsNamedColor -eq $true){
$ColorName = $label1.BackColor.Name
}
else {
$label1.BackColor = 'green'
$ColorName = $label1.BackColor.Name
}
[System.Windows.Forms.MessageBox]::Show($ColorName)

The original question was how to get the name of the color.
If a controls background (or any other color) was initialized with an ARGB value, the 'name' property will not return a name.
Even if the color matches an exact value of a named color.
It does however return the hex codes for the color. ffffff for example for white

That is documented for the underlying .NET type. So if in doubt, get the type of an object and refer to the documentation :D

So you cannot rely on a color always being named. Just use the RGB value to store a previous color.

I personally use the debug console if I need to know what an object is made of so to speak.
DebugConsole.png
DebugConsole.png (17.61 KiB) Viewed 2921 times
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Problem getting the current BackColor of a textbox object

Post by Alexander Riedel »

Seems like you can use another .NET type for a conversion:
if($label1.BackColor.IsNamedColor -eq $false) {
$BackColor = $label1.BackColor.Name
$BackColor = [System.Drawing.ColorTranslator]::FromHtml('#'+$BackColor)
}

Or at least that's what Google said :D
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Re: Problem getting the current BackColor of a textbox object

Post by FrankAndrew »

Hi SAPIEN Team,

I would like to thank all of you for the FANTASTIC help in understanding more about the workings of the System.Drawing.Color object!

I was looking mainly in the Watch Panel to see the details about my variable. As you know it is NOT the same thing as the Variables Panel:
PSS Watch Panel
PSS Watch Panel
2019.07.18_08-45-42_PSS_Watch_Panel.png (19.1 KiB) Viewed 2910 times
PSS Variables Panel
PSS Variables Panel
2019.07.18_08-41-26_PSS_Variables_Panel.png (41.15 KiB) Viewed 2910 times
I also did NOT know that there is a special color name of "Control". That confused me, I thought that it was telling me that the variable was a Control and not the name of the color itself. As you will know that is NOT a name that can be used as such:

Code: Select all

[System.Drawing.Color]$Color = 'control'
I have attached here the test PSF file that I used to verify what all of you told me:
Test-GetBackColors.psf
Test Form PSF
(82.43 KiB) Downloaded 113 times
It would be GREAT if the Watch Panel would display the the "tree" of values for objects like the Variables Panel does but we can't have everything. :)

The main thing that I was trying to get was the BackColor for a TextBox that is flagged as ReadOnly so that I can reset that BackColor when clearing the value and resetting the Colors, ForeColor and BackColor.

I got the idea after read all of the updates last night and trying the suggestions, that the Main Windows BackColor is more than likely the same as a textboxes ReadOnly BackColor.

Thanks to all of the help that you have given me I WILL be able to accomplish what I was looking to do!

Again the SAPIEN Team is the BEST in the Software Industry! :D
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem getting the current BackColor of a textbox object

Post by jvierra »

There is a very simple way to reset the background or foreground color of any control.

"ResetBackColor()"
"ResetForeColor()"

These methods will set the control to the original configured color. All of the variables neeed are already built into the controls.

Here is a full demo. It takes only a couple of lines of code.
Attachments
Demo-SaveColorProperty.psf
(43.94 KiB) Downloaded 146 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem getting the current BackColor of a textbox object

Post by jvierra »

Here is the documentation reference for the above methods: https://docs.microsoft.com/en-us/dotnet ... mework-4.8

It is inherited from "Control" and is available on very control in WinForms.
User avatar
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Re: Problem getting the current BackColor of a textbox object

Post by FrankAndrew »

Hi jvierra,

Thanks a Million, as I said before, you guys & gals are the BEST!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem getting the current BackColor of a textbox object

Post by jvierra »

Just some added info that can be useful.

Every control in WinForms has a property called “Tag”. This property is used to “tag” a control with extra information that is always available on the control.

<control variable>.Tag = ‘any kind of object or value’

To access this baggage we can use “$this.Tag” inside any event for this object.

Example:

In the form “Load” event or at the root of the script add the information:

$textbox1.Tag = $textbox1.BackColor

In any event for this control:

Code: Select all

$textbox_Click = {
        $this.BAckColor = $this.Tag
}
I use this to carry objects that are useful in the script which allows us to store more than one piece of information.

Code: Select all

# at the root of the script
$tag = @{
    ForeColor = $null
    BackColor = $null
    OriginalText = ''
}
$textbox1.Tag = [pscustomobject]$tag
$textbox2.Tag = [pscustomobject]$tag
# and so on
Now each object's baggage can be accessed as either the object or “$this”.

$textbox1.Tag.BackColor
$this.Tag.OriginalText


This avoids the need for a large number of global variables and simplifies the naming as the names are the same on every object.

Isn’t OOP wonderful. Microsoft has done a very amazing job of designing object controls and forms. Everything is an object and everything is extensible.
User avatar
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Re: Problem getting the current BackColor of a textbox object

Post by FrankAndrew »

Hi jvierra,

That is EXCELLENT information, THANKS!

I will put that to good use. :D

Gruß,
Drew
This topic is 4 years and 8 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