当前位置:编程学习 > C#/ASP.NET >>

Color.FromName 咋能支持汉语? 本地语言问题

使用combobox做颜色选择功能,颜色名称取出时候,红蓝绿等常见颜色是汉语的而不是英语,
之后用Color.FromName()参数是汉语即不识别,无法转换成对应颜色。
如果取出System.Drawing.Color颜色列表是英文,或者Color.FromName 支持汉语即解决问题了。
 

//版本1 不好用
                //string[] colors = Enum.GetNames(typeof(System.Drawing.KnownColor));
                //foreach (string  s in colors )
                //{
                //    this.comboBox3.Items.Add(s);
                //    this.comboBox5.Items.Add(s);
                //    this.comboBox23.Items.Add(s);
                //    this.comboBox21.Items.Add(s);
                //    this.comboBox4.Items.Add(s);
                //    this.comboBox2.Items.Add(s);
                //    this.comboBox1.Items.Add(s);
                // }
            //版本2
            System.Collections.Generic.List<string> colors = new System.Collections.Generic.List<string>(); 
            Type t = typeof(System.Drawing.Color); 
            System.Reflection.PropertyInfo[] infos = t.GetProperties();
            string s;
            foreach (System.Reflection.PropertyInfo info in infos)
            {
                if (info.PropertyType == typeof(System.Drawing.Color))
                {
                   s=info.Name ;
                    this.comboBox3.Items.Add(s);
                    this.comboBox5.Items.Add(s);
                    this.comboBox23.Items.Add(s);
                    this.comboBox21.Items.Add(s);
                    this.comboBox4.Items.Add(s);
                    this.comboBox2.Items.Add(s);
                    this.comboBox1.Items.Add(s);
                }
            }


        void combobox单击(ComboBox b)
        {
             try
            {
               b.BackColor = Color.FromName(b.Text.ToString());
               

            }
            catch { };
        }

        void combobox自画(ComboBox b,  DrawItemEventArgs e)
        {
            Rectangle rc = new Rectangle(e.Bounds.X - 1, e.Bounds.Y,
          e.Bounds.Width - 1, e.Bounds.Height - 1);
            string colorName = b.Items[e.Index].ToString();
            e.Graphics.FillRectangle(new SolidBrush(Color.FromName(colorName)), rc);
        }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,