Page 2 of 2

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 11:35 pm
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 481 times

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 11:51 pm
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

Re: Problem getting the current BackColor of a textbox object

Posted: Thu Jul 18, 2019 12:12 am
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:
2019.07.18_08-45-42_PSS_Watch_Panel.png
PSS Watch Panel
2019.07.18_08-45-42_PSS_Watch_Panel.png (19.1 KiB) Viewed 470 times
2019.07.18_08-41-26_PSS_Variables_Panel.png
PSS Variables Panel
2019.07.18_08-41-26_PSS_Variables_Panel.png (41.15 KiB) Viewed 470 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 8 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

Re: Problem getting the current BackColor of a textbox object

Posted: Thu Jul 18, 2019 12:35 am
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.

Re: Problem getting the current BackColor of a textbox object

Posted: Thu Jul 18, 2019 12:38 am
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.

Re: Problem getting the current BackColor of a textbox object

Posted: Thu Jul 18, 2019 1:31 am
by FrankAndrew
Hi jvierra,

Thanks a Million, as I said before, you guys & gals are the BEST!

Re: Problem getting the current BackColor of a textbox object

Posted: Thu Jul 18, 2019 10:47 am
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.

Re: Problem getting the current BackColor of a textbox object

Posted: Thu Jul 18, 2019 11:29 pm
by FrankAndrew
Hi jvierra,

That is EXCELLENT information, THANKS!

I will put that to good use. :D

Gruß,
Drew