Click here to Skip to main content
14,299,849 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Folks,
I want to make a color from given text. say the user types in the color name in a text box. what I want is to change the form color according to the color typed in the text box. Say myform.backgroundcolor=?(what should I type in to convert text to the color )

Hope you are getting. don't need to use colordialog.
Thank you.
Regards
Posted
Updated 22-May-10 0:39am
v3
Comments
Sandeep Mewara 21-May-10 15:30pm
   
So what if the user types a color not in the list of system colors or so? If you don't want to give color dialog, give a dropdown of fixed colors and use the selected color.
Rate this:
Please Sign up or sign in to vote.

Solution 1

use

Color myColor = Color.FromName(textBox1.Text);
if (!(myColor.A == 0 && myColor.R == 0 && myColor.G == 0 &&
    myColor.B == 0))
{
   this.BackColor = myColor;
}


If the color is not a predefined name, then it will return 0 to all of the ARGB elements.
   
v3
Comments
Sandeep Mewara 21-May-10 15:42pm
   
Does it take care of all sorts of valid color names & validation(like caps)? Or will it be just System colors?
William Winner 21-May-10 15:48pm
   
I believe it has to be a KnownColor. These are found at http://msdn.microsoft.com/en-us/library/system.drawing.knowncolor.aspx

But, if the user typed "Light Blue", it wouldn't work because the KnownColor is "LightBlue". I agree that picking color based on what the user types isn't the best method.
Sandeep Mewara 21-May-10 15:59pm
   
Got it! Yet what you suggested looks a fit to what OP needs. Nice one.
Kunal Chowdhury «IN» 22-May-10 6:55am
   
You need to write a good algorithm in such case.
Rate this:
Please Sign up or sign in to vote.

Solution 3

There is plainly no API that cN magically decipher any terminology possible for any color. So, a drop down is what you need.
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

If a KnownColor is entered in a text box, you can actually do a conversion.

See here for a list of know colors.
   
Comments
William Winner 22-May-10 17:24pm
   
Reason for my vote of 2
While factually true, this doesn't actually help. The only solution on that page is to use the enum KnownColor to return a color or to use the RGB value, not a text value.

This answer added nothing to the previous answers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100