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.
User avatar
FrankAndrew
Posts: 128
Joined: Thu Oct 26, 2006 7:00 pm

Problem getting the current BackColor of a textbox object

Post by FrankAndrew » Wed Jul 17, 2019 1:43 am

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?

User avatar
brittneyr
Site Admin
Posts: 124
Joined: Thu Jun 01, 2017 7:20 am

Re: Problem getting the current BackColor of a textbox object

Post by brittneyr » Wed Jul 17, 2019 7:14 am

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
Brittney Ryn
SAPIEN Technologies, Inc.

User avatar
Alexander Riedel
Posts: 7001
Joined: Tue May 29, 2007 4:43 pm

Re: Problem getting the current BackColor of a textbox object

Post by Alexander Riedel » Wed Jul 17, 2019 7:45 am

2019-07-17_7-44-02.png
2019-07-17_7-44-02.png (17.96 KiB) Viewed 556 times
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
DrewEaston
Posts: 48
Joined: Mon Mar 12, 2007 4:43 pm

Re: Problem getting the current BackColor of a textbox object

Post by DrewEaston » Wed Jul 17, 2019 12:53 pm

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

User avatar
jvierra
Posts: 13682
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Problem getting the current BackColor of a textbox object

Post by jvierra » Wed Jul 17, 2019 1:09 pm

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.

User avatar
DrewEaston
Posts: 48
Joined: Mon Mar 12, 2007 4:43 pm

Re: Problem getting the current BackColor of a textbox object

Post by DrewEaston » Wed Jul 17, 2019 1:11 pm

Is it even possible to get windows to give us this value back?

User avatar
brittneyr
Site Admin
Posts: 124
Joined: Thu Jun 01, 2017 7:20 am

Re: Problem getting the current BackColor of a textbox object

Post by brittneyr » Wed Jul 17, 2019 1:23 pm

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:

Code: Select all

$oldColor = $textbox1.BackColor
$textbox1.BackColor = 'Green'


$oldColor = $textbox1.BackColor
$textbox1.BackColor = [System.Drawing.Color]::FromArgb(0, 153, 204)
$textbox1.Text = $oldColor.Name
Brittney Ryn
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 13682
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Problem getting the current BackColor of a textbox object

Post by jvierra » Wed Jul 17, 2019 1:25 pm

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.

User avatar
DrewEaston
Posts: 48
Joined: Mon Mar 12, 2007 4:43 pm

Re: Problem getting the current BackColor of a textbox object

Post by DrewEaston » Wed Jul 17, 2019 1:43 pm

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.
2019.07.17_10-34-44_Watch_Panel_PSS_with_BackColors.png
PSS Watch panel with BackColors
2019.07.17_10-34-44_Watch_Panel_PSS_with_BackColors.png (4.38 KiB) Viewed 507 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.

User avatar
jvierra
Posts: 13682
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Problem getting the current BackColor of a textbox object

Post by jvierra » Wed Jul 17, 2019 2:12 pm

Post the PSF of your test form. You are doing something odd somewhere.

Locked