在设计时支持下创建自定义控件
问题描述:
嗨.首先,很抱歉我的语言不清
在这段代码中,我在设计时创建了一个组件,属性ComplexObject(class)正确初始化了窗体,并在InitializeComponent()方法Form中使用了所有详细信息.
但是当我在表单上使用grid属性时,它没有在初始化component()方法Form中初始化.
是什么原因?
Hi. First, I''m sorry for my weak language
in this code i create a componet in design time ,property ComplexObject(class) correctly initialize on the my form with all detailes in the InitializeComponent() method Form.
But when I using the grid property on the form,it not initialize in the initialization component () method Form.
What is the reason?
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace WindowsFormsApplication18
{
[DefaultEvent("NumofClicksChanged")]
[DefaultProperty("NumOfClicks")]
[ToolboxBitmap(typeof(ButtonEx), "ButtonEx.png")]
public class ButtonEx : Button
{
private System.ComponentModel.Container components = null;
public ButtonEx()
{
components = new System.ComponentModel.Container();
_numOfClicks = 10;
_ComplexObject = new Info();
_grid = new DataGridView();
}
private int _numOfClicks;
public event EventHandler NumofClicksChanged;
[DefaultValue(10)]
[Category("Special Properties")]
[Description("Tells how many time the button has been clicked")]
[DisplayName("Number of Clicks")]
public int NumOfClicks
{
get { return _numOfClicks; }
set
{
if (_numOfClicks != value)
{
OnNumofClicksChanged(EventArgs.Empty);
_numOfClicks = value;
}
}
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
NumOfClicks++;
}
protected virtual void OnNumofClicksChanged(EventArgs eventArgs)
{
if (NumofClicksChanged != null)
{
NumofClicksChanged(this, eventArgs);
}
}
string s;
[Browsable(false)]
public string ComplexProperty
{
get { return s; }
set { s = value; }
}
private Color c;
[DefaultValue(typeof(Color), "#FFBA00")]
public System.Drawing.Color ColorProperty
{
get { return c; }
set { c = value; }
}
System.Drawing.Font f;
public System.Drawing.Font FontProperty
{
get
{
if (f == null)
return this.Font;
else return f;
}
set { f = value; }
}
void ResetFontProperty()
{
f = null;
}
bool ShouldSerializeFontProperty()
{
return f == null ? false : true;
}
private string spVar;
[ParenthesizePropertyName(true)]
public string SpecialProperty
{
get { return spVar; }
set { spVar = value; }
}
private int refreshOthers;
[RefreshProperties(RefreshProperties.All)]
public int RefreshOtherPropeties
{
get { return refreshOthers; }
set { refreshOthers = value; }
}
private string v;
[ReadOnly(true)]
public string ReadOnlyProperty
{
get { return v; }
set { v = value; }
}
private string v1;
[RefreshProperties(RefreshProperties.All)]
public string RefreshOtherProperty
{
get { return v1; }
set { v1 = value; }
}
private string v2;
[NotifyParentProperty(true)]
public string NotifyParentProperty
{
get { return v2; }
set { v2 = value; }
}
private string v3;
[DesignOnly(true)]
public string DesignTimeOnlyProperty
{
get { return v3; }
set { v3 = value; }
}
private Info _ComplexObject;
[TypeConverter(typeof(ComplexObjectConverter))]
public Info ComplexObject
{
get { return _ComplexObject; }
set { _ComplexObject = value; }
}
private System.Windows.Forms.DataGridView _grid;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof(DataGridViewConverter))]
public System.Windows.Forms.DataGridView grid
{
get { return _grid; }
set { _grid = value; }
}
}
public class Info
{
int i;
string s;
public int IntProperty
{
get { return i; }
set { i = value; }
}
public string StringProperty
{
get { return s; }
set { s = value; }
}
}
public class ComplexObjectConverter:System.ComponentModel.TypeConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(Info));
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}
public class DataGridViewConverter : System.ComponentModel.TypeConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(DataGridView));
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}
}
答
Google展示了所有内容,甚至包括CodeProject上的文章:
创建自定义控件-提供设计时间支持1 [创建自定义控件-提供设计时间支持2 [
Google reveals all, even articles here on CodeProject:
Creating Custom Controls-Providing Design Time Support 1[^]
Creating Custom Controls-Providing Design Time Support 2[^]