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 » Wed Apr 03, 2019 2:02 pm

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

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

Re: List all WebColors in a drop down

Post by jvierra » Wed Apr 03, 2019 2:58 pm

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 » Thu Apr 04, 2019 6:39 am

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 » Thu Apr 04, 2019 7:41 am

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 » Thu Apr 04, 2019 7:42 am

Thanks David.. But I'm looking for something like this
Attachments
ComboBox.png
ComboBox.png (94.88 KiB) Viewed 545 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 » Thu Apr 04, 2019 8:28 am

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.

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

Re: List all WebColors in a drop down

Post by jvierra » Thu Apr 04, 2019 8:59 am

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 » Thu Apr 04, 2019 10:16 am

I used a combination of both and it's working!

I'm so thankful

Code: Select all

$colors = ((([System.Drawing.Color]).GetProperties()) |
Where-Object{ $_.PropertyType -eq [System.Drawing.Color] }).name
$colors += ((([System.Drawing.SystemColors]).GetProperties()) |
Where-Object{ $_.PropertyType -eq [System.Drawing.Color] }).name
$colors = $colors | Sort-Object
Update-ComboBox -ComboBox $combobox1 -Items $colors

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

Re: List all WebColors in a drop down

Post by jvierra » Thu Apr 04, 2019 10:28 am

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.

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

Re: List all WebColors in a drop down

Post by jvierra » Thu Apr 04, 2019 11:35 am

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

Locked