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
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Problem getting the current BackColor of a textbox object

Post 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?
User avatar
brittneyr
Site Admin
Posts: 1654
Last visit: Wed Mar 27, 2024 1:54 pm
Answers: 39
Been upvoted: 30 times

Re: Problem getting the current BackColor of a textbox object

Post by brittneyr »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
Brittney
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 »

2019-07-17_7-44-02.png
2019-07-17_7-44-02.png (17.96 KiB) Viewed 4274 times
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
DrewEaston
Posts: 48
Last visit: Thu Mar 30, 2023 8:47 am

Re: Problem getting the current BackColor of a textbox object

Post 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
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 »

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
Last visit: Thu Mar 30, 2023 8:47 am

Re: Problem getting the current BackColor of a textbox object

Post by DrewEaston »

Is it even possible to get windows to give us this value back?
User avatar
brittneyr
Site Admin
Posts: 1654
Last visit: Wed Mar 27, 2024 1:54 pm
Answers: 39
Been upvoted: 30 times

Re: Problem getting the current BackColor of a textbox object

Post 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
Brittney
SAPIEN Technologies, Inc.
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 »

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
Last visit: Thu Mar 30, 2023 8:47 am

Re: Problem getting the current BackColor of a textbox object

Post 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 4225 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.
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 »

Post the PSF of your test form. You are doing something odd somewhere.
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