aah, I'm getting dumb :D
I want to get a list of all the colors in Drawing.Color and perhaps add them to a listbox. How can I do this?
Printable View
aah, I'm getting dumb :D
I want to get a list of all the colors in Drawing.Color and perhaps add them to a listbox. How can I do this?
Code:Imports System.Drawing.Color
'---------------------
Color.YellowGreen()
eeeh??????!!!!Quote:
Originally posted by pirate
Code:Imports System.Drawing.Color
'---------------------
Color.YellowGreen()
I said I want to list them in a list box. Typing all of them is for sure a dumb thing to do:D there is a way to get a list of all of them by code, I just dont remember it
well yeah I tried that, but GetNames and GetValues arent members of Color. Color is a type :confused:
Code://**************************************
//
// Name: Use Reflection to list all Colo
// r objects
// Description:This is a good example in
// the power and flexibility of using Refle
// ction. This example will display in a co
// nsole a complete list of all the Colors
// available in the System.Drawing.Color na
// mespace. Put into a file colors.vb and c
// ompile at the command line using 'vbc co
// lors.vb' then run the colors.exe
// By: Chris Andersen
//
//This code is copyrighted and has // limited warranties.Please see http://
// www.Planet-Source-Code.com/xq/ASP/txtCod
// eId.482/lngWId.10/qx/vb/scripts/ShowCode
// .htm //for details. //**************************************
//
Imports System
Imports System.Reflection
Public Module ListColors
Public Sub Main(ByVal args() As String)
GetColors()
End Sub
Public Sub GetColors()
Dim asm As [Assembly] ' Assembly is reserved vb word so use the brackets to override
Dim myClasses() As Type
Dim myProperties() As PropertyInfo
Dim myTypeInfo As Type
Dim myPropertyInfo As PropertyInfo
' Lets load the System.Drawing dll
asm = [Assembly].LoadWithPartialName("System.Drawing")
If Not(asm Is Nothing) Then
' get an array of base classes
myClasses = asm.GetTypes()
End If
' Loop through each class
For Each myTypeInfo In myClasses
' if it is the color class
If myTypeInfo.ToString() = "System.Drawing.Color" Then
' get an array of all the properties
myProperties = myTypeInfo.GetProperties()
' loop the properties
For Each myPropertyInfo In myProperties
' if the property returns a Color object, then lets output it
' to the console as their are some non color properties we
' dont want to list
If myPropertyInfo.PropertyType.ToString() = "System.Drawing.Color" Then
Console.Writeline(myPropertyInfo.Name)
End If
Next myPropertyInfo
End If
Next myTypeInfo
End Sub
End Module
Lethal's code should work, but also the colors are in the Drawing.KnownColors enum. Although if you want just the named ones then you'd have to test individually for IsSystemColor.
VB Code:
Public Function GetColorList() As ArrayList Dim nms() As String = Drawing.KnownColor.GetNames(GetType(Drawing.KnownColor)) Dim al As New ArrayList() Dim o As String For Each o In nms Dim clr As Drawing.Color = Drawing.Color.FromName(o) If Not clr.IsSystemColor Then al.Add(o) Next Return al End Function
Although I don't know if this or the reflection way is faster.
Here's a short method I figured out using reflection:
Code:private void button1_Click(object sender, System.EventArgs e)
{
System.Reflection.PropertyInfo[] colors = typeof(System.Drawing.Color).GetProperties
(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
foreach(System.Reflection.PropertyInfo color in colors)
{
this.listBox1.Items.Add ((System.Drawing.Color) color.GetValue(null,null));
}
}
ya wanna convert that to vb ?
Since you asked nicely..;)
Code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim properties() As System.Reflection.PropertyInfo = GetType(System.Drawing.Color).GetProperties _
(Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static)
Dim color As System.Reflection.PropertyInfo
For Each color In properties
ListBox1.Items.Add(color.GetValue(Nothing, Nothing))
Next
End Sub
Thanks!
anyone to Get the Color to just show the Color name and not
"color[Blue]"
?
Sure:
Code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim properties() As System.Reflection.PropertyInfo = GetType(System.Drawing.Color).GetProperties _
(Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static)
Dim color As System.Reflection.PropertyInfo
For Each color In properties
ListBox1.Items.Add(color.Name())
Next
End Sub
thank you
anyone know why when i used a listView it adds like 3 per row and not just one ?
One problem with the second code Lethal posted. some of the things that will show up in the listbox wont be colors. That is why in my code, the first code Lethal posted from psc, I have
If myPropertyInfo.PropertyType.ToString() = "System.Drawing.Color"
that makes sure the return type is an actual color type. So that is needed also.
in Lethal version, that translates to
colors.PropertyType.ToString()
Forgot to add that checks to my code. Thanks
hey...thanks all. I think I choose Edneeis way, it's shorter:D
umm, is there away to get the colors in order of their color? like when you want to change the color of a control, VS.NET's color dialog shows the colors in order (in the WEB section). Is it possible to do something like this?:rolleyes:
bump
and yet again, bump
I'm not at my home computer right now, but what kind of order are they in? If alphabetical, you could just dump the array into an array list and sort them that way.
no not alphabetical. For example LightBlue and Blue would have to be next to each other, because they're both blue. I guess it's hard to sort it that way:D
Maybe try sorting by RGB value???
Hi Mr. Polite.
I noticed, that the default palette for 256 colors, in VB .NET's own imageeditor they are sorted like this :
#000000
#800000
#808000
#000080
From this I can see, that first B as compared. If that is
equal, then G is compared, and if that is equal then R is
compared.
If you add this class to you project, and create an array
of colors, you can use this class to sort colors.
The code may not be the pretties, but it works.
VB Code:
Public Class ColorSorter Implements IComparer Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare Dim c1 As Color = CType(x, Color) Dim c2 As Color = CType(y, Color) Dim resR As Integer Dim resG As Integer Dim resB As Integer If c1.B < c2.B Then resB = -1 If c1.B = c2.B Then resB = 0 If c1.B > c2.B Then resB = 1 If resB <> 0 Then Return resB Else If c1.G < c2.G Then resG = -1 If c1.G = c2.G Then resG = 0 If c1.G > c2.G Then resG = 1 If resG <> 0 Then Return resG Else If c1.R < c2.R Then resR = -1 If c1.R = c2.R Then resR = 0 If c1.R > c2.R Then resR = 1 Return resR End If End If End Function End Class
Hopes this helps.