Page 1 of 2

Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 1:43 am
by FrankAndrew
Product, version and build: PowerShell Studio 2019 5.6.166
32 or 64 bit version of product: 64bit
Operating system: Windows 10 Enterprise (1809)
32 or 64 bit OS: 64bit

I am changing the BackColor of a textbox.
e.g.

Code: Select all

$textbox1.BackColor = 'Green'
And that works just fine. :)

Before I change it to the desired color I would like to be able to make a note of the current BackColor first. So I tried this:

Code: Select all

$textboxBackColorBefore = $textbox1.BackColor
BUT what I am getting back is a:

Code: Select all

[System.Drawing.Color]
object not a string simular to what I had assigned before.

I then tried this:

Code: Select all

$textbox1.BackColor | Get-Member
# and this
$textboxBackColorBefore | Get-Member
I could NOT find out where I am supposed get the BackColor in [string] form from.
I saw NO properties or Methods that I could use to get the desired value. :cry:

I would like to be able to reassign the previous BackColor so:

Code: Select all

$textbox1.BackColor = $textboxBackColorBefore
To restore the previous BackColor.

Can you help me here?

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 7:14 am
by brittneyr
[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 7:45 am
by Alexander Riedel
2019-07-17_7-44-02.png
2019-07-17_7-44-02.png (17.96 KiB) Viewed 4277 times

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 12:53 pm
by DrewEaston
Hi Alex,
Thanks for that info but that does not show me how to get the information directly from a textbox itself.
That only creates a new color object and the assignment:

Code: Select all

$textbox1.BackColor = 'Green'
does the same thing.
What I would like to know is how I get the equivilent so:

Code: Select all

$previousBackColor = $textbox1.BackColor

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 1:09 pm
by jvierra
The code saves the color in the variable:

[b}$previousBackColor = $textbox1.BackColor[/b]

Why do you think this doesn't work? I use it all of the time.

Note that the object is not a string. It is a "System.Drawing.Color" object.

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 1:11 pm
by DrewEaston
Is it even possible to get windows to give us this value back?

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 1:23 pm
by brittneyr
The color 'Green' is a system-defined color that is why you are able to write it as 'Green' rather than directly creating a System.Drawing.Color object.

Here is some more info on Color objects:
https://docs.microsoft.com/en-us/dotnet ... work-4.7.1

As for setting the background color for a textbox, the code you have should work:
  1.     $oldColor = $textbox1.BackColor
  2.     $textbox1.BackColor = 'Green'
  3.    
  4.    
  5.     $oldColor = $textbox1.BackColor
  6.     $textbox1.BackColor = [System.Drawing.Color]::FromArgb(0, 153, 204)
  7.     $textbox1.Text = $oldColor.Name

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 1:25 pm
by jvierra
DrewEaston wrote: Wed Jul 17, 2019 1:11 pm Is it even possible to get windows to give us this value back?
It is very hard to understand what you issue is. The variable will contain the current color object and it can ge re-assigned back to the control.

Why do you think this is not working?

If you want to know what is in the variable you can do this:

[system.windows.forms.messagebox]::Show($previousBackColor.Name)

Perhaps you have scope issues with your code.

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 1:43 pm
by DrewEaston
Hi Everyone,

Thanks for all the info.

The reason why I opened this incident was because when I tried the call:

Code: Select all

$previousBackColor = $textbox1.BackColor
in a specific form at work I was getting an error with the assignment and again when I tried to reassign it back.
(shame on me I did NOT make a note of the exact errors I just went to the Watch panel to investigate)
In the Watch panel during debugging I could not see any values that made sense.

I tried to look at the value that was store in this varibel in the Watch window during debugging and was not seeing the "name" of the color even when I entered:

Code: Select all

$previousBackColor.Name
In the watch window.
PSS Watch panel with BackColors
PSS Watch panel with BackColors
2019.07.17_10-34-44_Watch_Panel_PSS_with_BackColors.png (4.38 KiB) Viewed 4228 times
I just created a new small form with just a couple of textboxes and some button to test what happen.
(Thanks to jvierra for his comment that they do this all the time)
So it DOES work as I want it to just the values are NOT displayable in the Watch panel at this time.

I guess the main problem here is that the Watch panel does NOT let me see the details for this variable type.

Re: Problem getting the current BackColor of a textbox object

Posted: Wed Jul 17, 2019 2:12 pm
by jvierra
Post the PSF of your test form. You are doing something odd somewhere.