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.
User avatar
localpct
Posts: 271
Joined: Wed Sep 09, 2015 12:53 pm

ColorDialog

Post by localpct » Tue Jul 10, 2018 6:26 pm

Here is my script to change the background to my form.
  1. $uIColorPickerToolStripMenuItem_Click={
  2.  
  3.     #TODO: Place custom script here
  4.  
  5.     $colordialog1.ShowDialog()
  6.  
  7.     $StageBackColor = $colordialog1.Color.Name
  8.  
  9.     $tbxForm.BackColor = $StageBackColor
  10.  
  11. }
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... )

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

Re: ColorDialog

Post by jvierra » Tue Jul 10, 2018 7:02 pm

Use the color and not the name.

$colordialog1.ShowDialog()
$tbxForm.BackColor = $colordialog1.Color

User avatar
owinsloe
Posts: 49
Joined: Tue Jul 10, 2012 10:37 am

Re: ColorDialog

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

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

Re: ColorDialog

Post by jvierra » Tue Jul 10, 2018 7:55 pm

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.

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

Re: ColorDialog

Post by jvierra » Tue Jul 10, 2018 8:04 pm

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: 271
Joined: Wed Sep 09, 2015 12:53 pm

Re: ColorDialog

Post by localpct » Wed Jul 11, 2018 5:19 am

I really appreciate the effort.. Here is what I've determined you've said
BGColorPicker.psf
(53.02 KiB) Downloaded 8 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

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

Re: ColorDialog

Post by jvierra » Wed Jul 11, 2018 5:31 am

Sorry. I fixed the example so that it saves the string name and not the color object.

User avatar
localpct
Posts: 271
Joined: Wed Sep 09, 2015 12:53 pm

Re: ColorDialog

Post by localpct » Wed Jul 11, 2018 5:37 am

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 6 times

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

Re: ColorDialog

Post by jvierra » Wed Jul 11, 2018 5:54 am

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: 271
Joined: Wed Sep 09, 2015 12:53 pm

Re: ColorDialog

Post by localpct » Wed Jul 11, 2018 8:05 pm

Confirmed working! thanks

Locked