Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

use

C#
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.
 
Share this answer
 
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.
There is plainly no API that cN magically decipher any terminology possible for any color. So, a drop down is what you need.
 
Share this answer
 
If a KnownColor is entered in a text box, you can actually do a conversion.

See here for a list of know colors.
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900