当前位置: 移动技术网 > IT编程>开发语言>c# > c#遍历System.drawing.Color下面的所有颜色以及名称以查看

c#遍历System.drawing.Color下面的所有颜色以及名称以查看

2019年07月18日  | 移动技术网IT编程  | 我要评论

面试的时候被问到,如何遍历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();
         }
     }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网