C# 在组合框中显示颜色列表-颜色选择器
]'我想用所有颜色的列表填充我的C# 在组合框中显示颜色列表-颜色选择器,c#,.net,winforms,combobox,C#,.net,Winforms,Combobox,]'我想用所有颜色的列表填充我的组合框。我期待着这样的事情: CBcolor.DataSource = AllColor; 然后,我想像这样使用我的组合框: Color selected = CBcolor.selectedvalue; C_ObjetGraphique cercle = new dessin.Cercle(e.Location, selected, selected, 100); cercle.Affiche(); ledessin.ajoute(cercle); 如
组合框
。我期待着这样的事情:
CBcolor.DataSource = AllColor;
然后,我想像这样使用我的组合框:
Color selected = CBcolor.selectedvalue;
C_ObjetGraphique cercle = new dessin.Cercle(e.Location, selected, selected, 100);
cercle.Affiche();
ledessin.ajoute(cercle);
如何将我的
组合框中的颜色列表显示为颜色选择器?通常,您需要将颜色列表设置为组合框的数据源。您可能有一些预定义颜色的列表,如Color.Red、Color.Green、Color.Blue;您可以依赖KnownColor
,也可以使用反射来获取Color
类型的Color
属性
在本例中,我使用color
类型的颜色属性来显示如下组合框:
获取颜色列表并设置组合框的数据源:
comboBox1.DataSource = typeof(Color).GetProperties()
.Where(x => x.PropertyType == typeof(Color))
.Select(x => x.GetValue(null)).ToList();
comboBox1.MaxDropDownItems = 10;
comboBox1.IntegralHeight = false;
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.DrawItem += comboBox1_DrawItem;
if(comboBox1.SelectedIndex>=0)
this.BackColor = (Color)comboBox1.SelectedValue;
处理组合框的自定义绘图:
comboBox1.DataSource = typeof(Color).GetProperties()
.Where(x => x.PropertyType == typeof(Color))
.Select(x => x.GetValue(null)).ToList();
comboBox1.MaxDropDownItems = 10;
comboBox1.IntegralHeight = false;
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.DrawItem += comboBox1_DrawItem;
if(comboBox1.SelectedIndex>=0)
this.BackColor = (Color)comboBox1.SelectedValue;
然后对于组合框1\u DrawItem
:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index >= 0)
{
var txt = comboBox1.GetItemText(comboBox1.Items[e.Index]);
var color = (Color)comboBox1.Items[e.Index];
var r1 = new Rectangle(e.Bounds.Left + 1, e.Bounds.Top + 1,
2 * (e.Bounds.Height - 2), e.Bounds.Height - 2);
var r2 = Rectangle.FromLTRB(r1.Right + 2, e.Bounds.Top,
e.Bounds.Right, e.Bounds.Bottom);
using (var b = new SolidBrush(color))
e.Graphics.FillRectangle(b, r1);
e.Graphics.DrawRectangle(Pens.Black, r1);
TextRenderer.DrawText(e.Graphics, txt, comboBox1.Font, r2,
comboBox1.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}
}
从组合框中获取所选颜色:
comboBox1.DataSource = typeof(Color).GetProperties()
.Where(x => x.PropertyType == typeof(Color))
.Select(x => x.GetValue(null)).ToList();
comboBox1.MaxDropDownItems = 10;
comboBox1.IntegralHeight = false;
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.DrawItem += comboBox1_DrawItem;
if(comboBox1.SelectedIndex>=0)
this.BackColor = (Color)comboBox1.SelectedValue;
如何:var myColors=newlist();对于(inti=0;i<256;i++)myColors.Add(Color.FromArgb(255,i,i,i))。你也可以使用System.Drawing.SystemColor中的颜色。你能明确地告诉我们你正在使用的平台/技术(WinForms、WPF、WebForms…)和颜色类(是,还是,或其他…)吗?你好,是的,对不起,我在WinForm上,在顶部我写了:使用System.Drawing;是否正在查找?字符串[]colorStrs=typeof(Color).GetProperties()。其中(x=>x.PropertyType==typeof(System.Drawing.Color))。选择(x=>x.Name).ToArray();