Page 1 of 2

List all WebColors in a drop down

Posted: Wed Apr 03, 2019 2:02 pm
by JohnTitor
Hello,

I'm looking to list all web colors in a drop down so users can select it for my form.

I'll let write that color to a reg key, and load it thus making their selection permanant but messing with the colordialog, I can't seem to print them to a combobox

Re: List all WebColors in a drop down

Posted: Wed Apr 03, 2019 2:58 pm
by jvierra
There is a ColorDialog which does this.

Re: List all WebColors in a drop down

Posted: Thu Apr 04, 2019 6:39 am
by JohnTitor
Gotcha, I just don't know how to display all of these colors..

I see there are some examples in C.. looks like I need to learn to read C lol

Re: List all WebColors in a drop down

Posted: Thu Apr 04, 2019 7:41 am
by davidc
Add a ColorDialog to the designer and then set the following lines within your button's click event:

Code: Select all

$button1_Click={
	if ($colordialog1.ShowDialog() -eq 'OK')
	{
		$color = $colordialog1.Color		
	}
}
To get the result, use the Color property of the ColorDialog.

Re: List all WebColors in a drop down

Posted: Thu Apr 04, 2019 7:42 am
by JohnTitor
Thanks David.. But I'm looking for something like this

Re: List all WebColors in a drop down

Posted: Thu Apr 04, 2019 8:28 am
by davidc
Here is a quick way of doing that:

Code: Select all

	$colors = [System.Enum]::GetValues([System.Drawing.KnownColor]) | ForEach-Object { [System.Drawing.Color]::FromKnownColor($_) }
	Update-ComboBox -ComboBox $comboboxColors -Items $colors

Re: List all WebColors in a drop down

Posted: Thu Apr 04, 2019 8:59 am
by jvierra
For web colors only this should be the correct enum:

Code: Select all

(([System.Drawing.Color]).GetProperties()) | 
      Where-Object{ $_.PropertyType -eq [System.Drawing.Color] } | 
      Select-Object name
For SystemColors just change to "[System.Drawing.SystemColors]".

Use David's method for "Windows Colors".

Re: List all WebColors in a drop down

Posted: Thu Apr 04, 2019 10:16 am
by JohnTitor
I used a combination of both and it's working!

I'm so thankful
  1. $colors = ((([System.Drawing.Color]).GetProperties()) |
  2.  
  3.     Where-Object{ $_.PropertyType -eq [System.Drawing.Color] }).name
  4.  
  5.     $colors += ((([System.Drawing.SystemColors]).GetProperties()) |
  6.  
  7.         Where-Object{ $_.PropertyType -eq [System.Drawing.Color] }).name
  8.  
  9.     $colors = $colors | Sort-Object
  10.  
  11.     Update-ComboBox -ComboBox $combobox1 -Items $colors

Re: List all WebColors in a drop down

Posted: Thu Apr 04, 2019 10:28 am
by jvierra
But now you have no way to decode them. You can't tell which one is from which enum. It will work on objects that can decode all colors by string name. Some objects only manage certain color types.

Re: List all WebColors in a drop down

Posted: Thu Apr 04, 2019 11:35 am
by jvierra
Here is a quick and dirty sample of how to manage objects in a list/combo which can make colors easier.

We can also just draw a little sample of the color as an icon-like tag to the list.

In this demo the colors denote the type of the color string and the color objects are available.