namespace Rolends.CustomControl
{
public class CustomControls : WebControl
{
[Description("这是自定义数据测试"), Category("自定义数据"), DefaultValue("1,2"), Localizable(true), Browsable(true), Editor(typeof(CustomPropertyEditer), typeof(UITypeEditor))]
public CustomPropertyClass Caption { get; set; }
public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
{
writer.Write("this is Rolends Control");
base.RenderControl(writer);
}
}
[TypeConverter(typeof(PropertyConvert))]
public class CustomPropertyClass
{
[Browsable(true)]
public int Min { get; set; }
[Browsable(true)]
public int Max { get; set; }
public CustomPropertyClass(int min, int max)
{
this.Min = min;
this.Max = max;
}
public CustomPropertyClass()
{ }
}
public class PropertyConvert : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(String)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(String)) return true;
if (destinationType == typeof(InstanceDescriptor)) return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
String[] v = ((String)value).Split(',');
if (v.GetLength(0) != 2)
{
throw new ArgumentException("Invalid parameter format");
}
CustomPropertyClass csf = new CustomPropertyClass();
csf.Min = Convert.ToInt32(v[0]);
csf.Max = Convert.ToInt32(v[1]);
return csf;
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
String result = "";
if (destinationType == typeof(String))
{
CustomPropertyClass scope = (CustomPropertyClass)value;
result = scope.Min.ToString() + "," + scope.Max.ToString();
return result;
}
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(CustomPropertyClass).GetConstructor(new Type[] { typeof(Int32), typeof(Int32) });
CustomPropertyClass scope = (CustomPropertyClass)value;
return new InstanceDescriptor(ci, new object[] { scope.Min, scope.Max });
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(CustomPropertyClass), attributes);
}
}
public class CustomPropertyEditer : UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService editorService = null;
if (context != null && context.Instance != null && provider != null)
{
editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (editorService != null)
{
CustomControls control = (CustomControls)context.Instance;
CustomPropertyForm dlg = new CustomPropertyForm(control.Caption);
dlg.ShowDialog();
//if (dlg.ShowDialog() == DialogResult.OK)
//{
// value = dlg.Scope;
// return value;
//}
}
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
}
partial class CustomPropertyForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.txtMin = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtMax = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(99, 13);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(36, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 1;
this.label1.Text = "最小值:";
//
// txtMin
//
this.txtMin.Location = new System.Drawing.Point(99, 12);
this.txtMin.Name = "txtMin";
this.txtMin.Size = new System.Drawing.Size(100, 21);
this.txtMin.TabIndex = 0;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(36, 15);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 12);
this.label2.TabIndex = 1;
this.label2.Text = "最小值:";
//
// txtMax
//
this.txtMax.Location = new System.Drawing.Point(99, 51);
this.txtMax.Name = "txtMax";
this.txtMax.Size = new System.Drawing.Size(100, 21);
this.txtMax.TabIndex = 0;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(36, 54);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(53, 12);
this.label3.TabIndex = 1;
this.label3.Text = "最大值:";
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(50, 165);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 23);
this.btnOK.TabIndex = 2;
this.btnOK.Text = "确定";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// button1
//
this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.button1.Location = new System.Drawing.Point(173, 165);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "取消";
this.button1.UseVisualStyleBackColor = true;
//
// CustomPropertyForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button1);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtMax);
this.Controls.Add(this.txtMin);
this.Controls.Add(this.textBox1);
this.Name = "CustomPropertyForm";
this.Text = "CustomPropertyForm";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtMin;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtMax;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button button1;
}
public partial class CustomPropertyForm : Form
{
public CustomPropertyForm(CustomPropertyClass property)
{
InitializeComponent();
ControlProperty = property;
}
public CustomPropertyClass ControlProperty { get; private set; }
private void btnOK_Click(object sender, EventArgs e)
{
ControlProperty.Min = Convert.ToInt32(txtMin.Text);
ControlProperty.Max = Convert.ToInt32(txtMax.Text);
}
}
}
HAPPY EVERY DAY ! !