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.
This topic is 4 years and 9 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
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

List all WebColors in a drop down

Post by apowershelluser »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: List all WebColors in a drop down

Post by jvierra »

There is a ColorDialog which does this.
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: List all WebColors in a drop down

Post by apowershelluser »

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
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

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
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: List all WebColors in a drop down

Post by apowershelluser »

Thanks David.. But I'm looking for something like this
Attachments
ComboBox.png
ComboBox.png (94.88 KiB) Viewed 3520 times
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: List all WebColors in a drop down

Post by apowershelluser »

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

I'm so thankful
  1. $colors = ((([System.Drawing.Color]).GetProperties()) |
  2.     Where-Object{ $_.PropertyType -eq [System.Drawing.Color] }).name
  3.     $colors += ((([System.Drawing.SystemColors]).GetProperties()) |
  4.         Where-Object{ $_.PropertyType -eq [System.Drawing.Color] }).name
  5.     $colors = $colors | Sort-Object
  6.     Update-ComboBox -ComboBox $combobox1 -Items $colors
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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 136 times
This topic is 4 years and 9 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