c#遍历System.drawing.Color下面的所有颜色以及名称以查看
程序员文章站
2023-12-12 19:39:10
面试的时候被问到,如何遍历system.drawing.color下面的所有颜色以及名称以查看,当时答得不好,现将方案记录如下:
复制代码 代码如下:view code...
面试的时候被问到,如何遍历system.drawing.color下面的所有颜色以及名称以查看,当时答得不好,现将方案记录如下:
复制代码 代码如下:
view code
public partial class form1 : form
{
flowlayoutpanel newpanel = new flowlayoutpanel();
public form1()
{
initializecomponent();
newpanel.autoscroll = true;
//newpanel.flowdirection = flowdirection.bottomup;
//newpanel.wrapcontents = false;
newpanel.dock = dockstyle.fill;
newpanel.backcolor = color.white;
button1.anchor = (anchorstyles.bottom | anchorstyles.right);
}
private void button1_click(object sender, eventargs e)
{
newpanel.controls.clear();
int i = 1;
foreach (var item in typeof(color).getmembers())
{
if (item.membertype == system.reflection.membertypes.property && system.drawing.color.fromname(item.name).isknowncolor == true)//只取属性且为属性中的已知color,剔除byte属性以及一些布尔属性等(a b g r isknowncolor name等)
{
label mylable = new label();
mylable.autosize = true;
mylable.backcolor = system.drawing.color.fromname(item.name);
mylable.text = system.drawing.color.fromname(item.name).name;
newpanel.controls.add(mylable);
//newpanel.getflowbreak(mylable);
i++;
}
}
this.controls.add(newpanel);
button1.text = i.tostring();
}
}