Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
which namespace must use for the button text colors and background colors?(since the buttons are dynamically generated in the code i need the namespace for that)
either it is System.Drawing.Design or System.Drawing.Color??

when i use the "System.Drawing.Design" namespace it gives the error as
"Cannot implicitly convert type "string" to System.Drawing.Color"

and yet "System.Drawing.Color" is not available.

any one can please me!
Posted

How about simply setting the Color Property of your Button...?
C#
Button btn = new Button();
btn.Location = //whatever location.
btn.BackColor = System.Drawing.Color.Red;
this.Controls.Add(btn);
// etc.


Make sure you have the System.Drawing assembly referenced in your project.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-May-11 17:02pm    
Correct, 5.
See just my notes to complement this answer.
--SA
Manfred Rudolf Bihy 26-May-11 18:40pm    
d'accord! 5+
Sander Rossel 1-Jun-11 2:03am    
Thanks :)
This is not about name space. Name spaces do not provide or limit access, they only affect how we name the typed. Normally, you never need to use System.Drawing.Design. Your problem is that the color is nothing like string. You can get color from the predefined set as Naerling demonstrated or make if from integer ARGB components, see System.Drawing.Color.FromArgb, http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx[^].

—SA
 
Share this answer
 
v3
Comments
Sander Rossel 26-May-11 18:40pm    
Thanks for your 5. Your answer complements mine perfectly.
I should have given you a vote of 1 for misspelling my name though. You forgot the 'e', so I took the liberty of correcting it. This time you are forgiven ;)
My 5 for the answer.
Sergey Alexandrovich Kryukov 26-May-11 20:32pm    
Thank you very much.
As to the name -- this is my bad. I hate is someone misspell anyone's name, no matter what. Thanks for the fix.
--SA
Manfred Rudolf Bihy 26-May-11 18:41pm    
Fived!
Sergey Alexandrovich Kryukov 26-May-11 20:33pm    
Thank you, Manfred.
--SA
you can use static fields which is returning an object of Color class...
or
you can use one static method named FromArgb(int red,int green,int blue) of Color class to adjust your own color...
Button _temp = new Button();
_temp.Name = "yourButtonName";
_temp.BackColor = System.Drawing.Color.FromArgb(255,0,0); // red color
_temp.ForeColor = System.Drawing.Color.FromArgb(0,255,0); // green color
this.Controls.Add(_temp);
 
Share this answer
 

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