List all WebColors in a drop down

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
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

List all WebColors in a drop down

Post 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

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: List all WebColors in a drop down

Post by jvierra »

There is a ColorDialog which does this.

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: List all WebColors in a drop down

Post 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

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: List all WebColors in a drop down

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

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: List all WebColors in a drop down

Post by JohnTitor »

Thanks David.. But I'm looking for something like this
Attachments
ComboBox.png
ComboBox.png (94.88 KiB) Viewed 1692 times

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: List all WebColors in a drop down

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

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: List all WebColors in a drop down

Post 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".

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: List all WebColors in a drop down

Post 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

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: List all WebColors in a drop down

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

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: List all WebColors in a drop down

Post 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.
Attachments
Demo=CBColors.psf
(10.14 KiB) Downloaded 48 times

Locked