哪个名称空间适合动态创建的按钮(以获取其背景颜色和文本颜色) [英] which namespace is suitable for dynamically created buttons (to get their background colors and text colors)

查看:97
本文介绍了哪个名称空间适合动态创建的按钮(以获取其背景颜色和文本颜色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按钮文本颜色和背景颜色必须使用哪个命名空间?(由于按钮是在代码中动态生成的,因此我需要该名称空间)
是System.Drawing.Design还是System.Drawing.Color ??

当我使用"System.Drawing.Design"命名空间时,它给出错误为
无法将类型字符串"隐式转换为System.Drawing.Color"

但"System.Drawing.Color"不可用.

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!

推荐答案

如何简单地设置Button的Color属性...?
How about simply setting the Color Property of your Button...?
Button btn = new Button();
btn.Location = //whatever location.
btn.BackColor = System.Drawing.Color.Red;
this.Controls.Add(btn);
// etc.



确保您的项目中引用了System.Drawing程序集.



Make sure you have the System.Drawing assembly referenced in your project.


这与名称空间无关.命名空间不提供或限制访问,它们仅影响我们对键入的名称的命名方式.通常,您不需要使用System.Drawing.Design.您的问题是颜色与string完全不同.您可以从Naerling演示的预定义集中获取颜色,也可以从整数ARGB组件获取颜色,请参见System.Drawing.Color.FromArgb
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


您可以使用静态字段,该字段将返回Color类的对象...

您可以使用Color类的一种名为FromArgb(int red,int green,int blue)的静态方法来调整自己的颜色...
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);


这篇关于哪个名称空间适合动态创建的按钮(以获取其背景颜色和文本颜色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆