[英]How to add a custom property to a UserControl which display as a combo box at the design time
Edit: The question is about adding a property during the design time , not during the runtime . 编辑:问题是在设计时添加属性,而不是在运行时 。
I created a UserControl, in which contains a button, and I want to add a new property that when user uses it in form, he could see the property FillColor
in user control's Property Tab
. 我创建了一个UserControl,其中包含一个按钮,我想添加一个新属性,当用户在表单中使用它时,他可以在用户控件的
Property Tab
看到属性FillColor
。 What's more, that property should be in the form of a combobox, which allows user to select the Color
in System.Drawing.Color. 更重要的是,该属性应该采用组合框的形式,允许用户在System.Drawing.Color中选择
Color
。
For Example: 例如:
I call my UserControl HalfFill
. 我打电话给我的UserControl
HalfFill
。 What it does is to fill a normal button from start to the half of he size. 它的作用是从正常按钮填充正常按钮到其大小的一半。
Now I want 2 properties to be customized using Property Tab
: 现在我想使用
Property Tab
自定义2个属性:
And when user chooses the color , the choices are listed in combobox (behaves like BackColor
property for normal buttons), and all of them come from System.Drawing.Color. 当用户选择颜色时,选项列在组合框中(行为类似于普通按钮的
BackColor
属性),并且所有选项都来自System.Drawing.Color。
I want to do it with Enum. 我想和Enum一起做。 Can someone help me?
有人能帮我吗? How i put the comboxBox with its values into the
Properties Tab
? 我如何将comboxBox及其值放入
Properties Tab
?
Here is an example that I am looking for 这是我正在寻找的一个例子
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+"");
}
}
following code will give you an idea how you can bind the combo using System.Drawing.Color. 下面的代码将告诉您如何使用System.Drawing.Color绑定组合。 Also need to use namespace using System.Reflection;
还需要使用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);
}
That my last version... now i only need to bind the comboBox values (that in the combBox) to the properties when i try that it show 那是我的最后一个版本...现在我只需要在尝试显示它时将comboBox值(在combBox中)绑定到属性
ChooseColor1 | ChooseColor1 | None
没有
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
}
Do you mean the property in the Designer
? 你的意思是
Designer
的财产吗? Please take a look at my example below. 请看下面我的例子。
I created a UserControl
called ColorTextBox
, and defined a property called FilledColor
. 我创建了一个名为
ColorTextBox
的UserControl
,并定义了一个名为FilledColor
的属性。 In the designer, I am able to change the color in a combo box: 在设计器中,我可以更改组合框中的颜色:
The property is shown in the Property Tab
: 该属性显示在“
Property Tab
:
Colors in the combobox, Drawing.Colors are under Web
tab: 组合框中的颜色,Drawing.Colors在
Web
选项卡下:
After change the FilledColor
to Highlight
: 将
FilledColor
更改为Highlight
:
My code is fairly simple: 我的代码很简单:
private Color _filledColor;
public ColorTextBox()
{
InitializeComponent();
_filledColor = Color.FromKnownColor(KnownColor.Control);
}
public Color FilledColor
{
get { return _filledColor; }
set
{
_filledColor = value;
button1.BackColor = _filledColor;
}
}
The only change is to change your property type to Color, and the magic completed. 唯一的变化是将您的属性类型更改为Color,并完成魔术。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.