ColorDialog

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 5 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
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

ColorDialog

Post by localpct »

Here is my script to change the background to my form.
  1. $uIColorPickerToolStripMenuItem_Click={
  2.     #TODO: Place custom script here
  3.     $colordialog1.ShowDialog()
  4.     $StageBackColor = $colordialog1.Color.Name
  5.     $tbxForm.BackColor = $StageBackColor
  6. }
Here is my error
ERROR: Exception setting "BackColor": "Cannot convert value "ffff8040" to type "System.Drawing.Color". Error: "ffff8040 is not a valid value for Int32.""
MainForm.psf (639, 2): ERROR: At Line: 639 char: 2
ERROR: + $tbxForm.BackColor = $StageBackColor
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
ERROR: + FullyQualifiedErrorId : ExceptionWhenSetting
ERROR:

Also, I'd like to store this value so the next time I open my form, its set.. So maybe a reg key or file. Preferably a .txt file. This does work with basic colors ( Red, Black, White, Etc... )
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ColorDialog

Post by jvierra »

Use the color and not the name.

$colordialog1.ShowDialog()
$tbxForm.BackColor = $colordialog1.Color
User avatar
owinsloe
Posts: 161
Last visit: Tue Mar 26, 2024 8:14 pm
Been upvoted: 1 time

Re: ColorDialog

Post by owinsloe »

Storing the color across sessions...this might help
https://trevorsullivan.net/2016/07/25/p ... variables/
Good luck
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ColorDialog

Post by jvierra »

owinsloe wrote: Tue Jul 10, 2018 7:16 pm Storing the color across sessions...this might help
https://trevorsullivan.net/2016/07/25/p ... variables/
Good luck
To clarify - the "BackColor" property is a color object. A color object can be set to a color object and not a string unless that string is the name of one of the enumerated system color objects.

[System.Drawing.Color]::Red or [System.Drawing.Color]'Red' or [System.Drawing.Color]::FromName('red')

It can also be set to a new color object:
[System.Drawing.Color]::FromArgb(int alpha, int red, int green, int blue)

$colorDialog1.Color.Name
is either the name of a defined system color or a hex string. This can be converted as follows:

[System.Drawing.Color]::FromName($colorDialog.Color.Name)

There is nothing here that relates to any kind of "session" so what you are suggesting is unclear. What "session" are you referring to? Why post an article on environment variables. Forms properties are persistent until the form is destroyed.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ColorDialog

Post by jvierra »

Ok - I missed the second question.

In forms we normally use a config file to persist settings.

Code: Select all

$colors = @{
    Form = @{
        Color = $tbxForm.Color.Name
        BackColor = $tbxForm.BackColor.Name
    }
    myTextBox = @{
        Color = $myTextBox.Color
        BackColor = $myTextBox.BackColor.Name
    }
}

# to save
$colors | Export-Clixml settings.clixml

# to load
$colors = Import-Clixml settings.clixml

To get the color:
$tbxForm.BackColor = [System.Drawing.Color]::FromName($color.Form.Color)

You can also use XML or a simple text file.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: ColorDialog

Post by localpct »

I really appreciate the effort.. Here is what I've determined you've said
BGColorPicker.psf
(53.02 KiB) Downloaded 144 times
ERROR: Exception setting "BackColor": "Cannot convert value "Color [A=255, R=0, G=255, B=64]" to type "System.Drawing.Color". Error: "Cannot convert the "Color
ERROR: [A=255, R=0, G=255, B=64]" value of type "Deserialized.System.Drawing.Color" to type "System.Drawing.Color".""
MainForm.psf (144, 2): ERROR: At Line: 144 char: 2
ERROR: + $tbxForm.BackColor = $colors.Form.BackColor
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
ERROR: + FullyQualifiedErrorId : ExceptionWhenSetting
ERROR:

*** PowerShell Script finished. ***
>> Execution time: 00:00:11
>> Script Ended
>> Max. CPU: 8 % Max. Memory: 33.29 MB
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ColorDialog

Post by jvierra »

Sorry. I fixed the example so that it saves the string name and not the color object.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: ColorDialog

Post by localpct »

ERROR: Exception setting "BackColor": "Control does not support transparent background colors."
BGColorPicker.psf (5, 2): ERROR: At Line: 5 char: 2
ERROR: + $tbxForm.BackColor = [System.Drawing.Color]::FromName($colors ...
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
ERROR: + FullyQualifiedErrorId : ExceptionWhenSetting
ERROR:
BGColorPicker.psf
(53.11 KiB) Downloaded 132 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ColorDialog

Post by jvierra »

The following works as expected:

Code: Select all

$tbxform_Load={
	$colors = Import-Clixml d:\Test\settings.clixml
	$tbxForm.BackColor = [System.Drawing.Color]::FromArgb($colors.Form.BackColor)

}

$button1_Click={
	$colordialog1.ShowDialog()
	$tbxForm.BackColor = $colordialog1.Color
	$colors = @{
		Form   = @{
			BackColor  = $tbxForm.BackColor.ToArgb()
		}
	}
	
	# to save
	$colors | Export-Clixml d:\Test\settings.clixml
}
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: ColorDialog

Post by localpct »

Confirmed working! thanks
This topic is 5 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