How to get name all colors from class Drawing::Color
zsukal (19) Mar 21, 2010 at 10:52am UTC
I want to get all names colors from class System::Drawing::Color.
I searching in forums this and else in network but samples whose I find not work in Visual Studio C++ 2008. Problem is witch typeof -
if i use samples
Type colorType = typeof(System.Drawing.Color);
them i have problem witch typeof.
I writing:
1 2
System::Drawing::Color cl;
array<String^>^ type = System::Enum::GetNames(cl.GetType());
but if i run i have error - why?
Can you help me.
Last edited on Mar 21, 2010 at 10:52am UTC
NGen (234) Mar 21, 2010 at 10:52am UTC
What library are you using? What error are you getting? What are you trying to do?
zsukal (19) Mar 21, 2010 at 10:52am UTC
I use library (my program is console app):
1 2 3 4 5 6 7
#using <mscorlib.dll>
#using <System.Drawing.dll>
...
System::Drawing::Color cl;
array<String^>^ type = System::Enum::GetNames(cl.GetType());
...
If I delete line array<String^>^... program go to end without error.
I want to get all names of color in class Color, and print it to screan.
Program is ended error:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Type provided must be an Enum.
Last edited on Mar 21, 2010 at 10:52am UTC
NGen (234) Mar 21, 2010 at 10:52am UTC
... You ARE using C++, right? I've never seen the 'using' pre-processor directive before, nor have I ever seen syntax as is being used when declaring your 'type' variable. I suggest you wait until someone who knows this stuff comes on.
Bazzy (4118) Mar 21, 2010 at 10:52am UTC
That is C++/CLI, not C++
writetonsharma (832) Mar 21, 2010 at 10:52am UTC
:O ....
Grey Wolf (1615) Mar 21, 2010 at 10:52am UTC
Here is some ugly code to play with...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
using namespace System;
using namespace System::Drawing;
int main(array<System::String ^> ^args)
{
System::Array^ colorsArray = Enum::GetValues( KnownColor::typeid );
array<KnownColor>^allColors = gcnew array<KnownColor>(colorsArray->Length);
Array::Copy( colorsArray, allColors, colorsArray->Length );
array<System::String ^>^allColorsNames = gcnew array<System::String ^>(colorsArray->Length);
for ( int i = 0; i < allColors->Length; i++ )
{
allColorsNames[ i ] = allColors[ i ].ToString();
Console::WriteLine(allColorsNames[ i ]);
}
return 0;
}
zsukal (19) Mar 21, 2010 at 10:52am UTC
Thanks.
I must make small modification and work good ;-):
1 2 3 4 5 6 7 8 9 10 11 12 13
System::Array^ colorsArray = Enum::GetValues( System::Drawing::KnownColor::typeid );
array<System::Drawing::KnownColor>^allColors = gcnew array<System::Drawing::KnownColor>(colorsArray->Length);
Array::Copy( colorsArray, allColors, colorsArray->Length );
array<System::String ^>^allColorsNames = gcnew array<System::String ^>(colorsArray->Length);
for ( int i = 0; i < allColors->Length; i++ )
{
allColorsNames[ i ] = allColors[ i ].ToString();
Console::WriteLine(allColorsNames[ i ]);
}
This topic is archived - New replies not allowed.