修改 问题是在设计时添加属性,而不是在运行时添加 。
我创建了一个UserControl,其中包含一个按钮,我想添加一个新属性,当用户在表单中使用它时,他可以在用户控件的FillColor
中看到属性Property Tab
。更重要的是,该属性应该采用组合框的形式,允许用户在System.Drawing.Color中选择Color
。
例如:
我调用了UserControl HalfFill
。它的作用是从正常按钮填充正常按钮到其大小的一半。
现在我希望使用Property Tab
自定义2个属性:
当用户选择颜色时,选项列在组合框中(对于普通按钮的行为类似BackColor
属性),并且所有选项都来自System.Drawing.Color。
我想用Enum做这件事。有人能帮我吗?我如何将comboxBox及其值放入Properties Tab
?
这是我正在寻找的一个例子
public partial class HalfButton: UserControl
{
public ComboBox ChooseColor //of all color in System.Drawing.Color
{
/* must be in that comboBox all the color in Color library so the user could choose
from properies of the button the color he want */
get { return x.Item[colorfill.ToString()); }
set { colorfill = Color.FromName(value); OnPaint(null); }
}
private Color colorfill = Color.Tomato;
ComboBox x;
public HalfButton()
{
InitializeComponent();
x = new ComboBox();
ArrayList ColorList = new ArrayList();
Type colorType = typeof(System.Drawing.Color);
PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static |
BindingFlags.DeclaredOnly |
BindingFlags.Public);
foreach (PropertyInfo c in propInfoList)
{
x.Items.Add(c.Name);
}
}
protected override void OnPaint(PaintEventArgs e)
{
MessageBox.Show(colorfill+"");
}
}
答案 0 :(得分:1)
以下代码将告诉您如何使用System.Drawing.Color绑定组合。 还需要使用System.Reflection;
来使用命名空间ArrayList ColorList = new ArrayList();
Type colorType = typeof(System.Drawing.Color);
PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
foreach (PropertyInfo c in propInfoList)
{
this.comboBox1.Items.Add(c.Name);
}
答案 1 :(得分:1)
我的上一个版本...... 现在我只需要将comboBox值(在combBox中)绑定到属性 当我尝试它显示
ChooseColor1 |无
public static ComboBox ComboFill()
{
ComboBox xx = new ComboBox();
ArrayList ColorList = new ArrayList();
Type colorType = typeof(System.Drawing.Color);
PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
foreach (PropertyInfo c in propInfoList)
{
xx.Items.Add(c.Name);
}
return xx;
}
private Color colorfill1 = Color.Tomato;
private Color colorfill2 = Color.Tomato;
private ComboBox x1=ComboFill();
private ComboBox x2=ComboFill();
public ComboBox ChooseColor1 {
get { return x1; }
set { x1=value; OnPaint(null); }
}
public ComboBox ChooseColor2 {
get { return x2; }
set { x2=value; OnPaint(null); }
}
protected override void OnPaint(PaintEventArgs e)
{
//my functions
//something with colorfill1
//something with colorfill2
}
答案 2 :(得分:1)
你的意思是Designer
中的财产吗?请看下面的例子。
我创建了一个名为UserControl
的{{1}},并定义了一个名为ColorTextBox
的属性。在设计器中,我可以在组合框中更改颜色:
该属性显示在FilledColor
:
组合框中的颜色,Drawing.Colors位于Property Tab
标签下:
将Web
更改为FilledColor
:
我的代码非常简单:
Highlight
唯一的变化是将您的属性类型更改为Color,并完成魔术。