namespace Rolends.CustomControl
{
    
public class CustomControls : WebControl
    {
        [Description(
"这是自定义数据测试"), Category("自定义数据"), DefaultValue("1,2"), Localizable(true), Browsable(true), Editor(typeof(CustomPropertyEditer), typeof(UITypeEditor))]
        
public CustomPropertyClass Caption { getset; }

        
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 { getset; }
        [Browsable(
true)]
        
public int Max { getset; }

        
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(9913);
            
this.textBox1.Name = "textBox1";
            
this.textBox1.Size = new System.Drawing.Size(10021);
            
this.textBox1.TabIndex = 0;
            
// 
            // label1
            // 
            this.label1.AutoSize = true;
            
this.label1.Location = new System.Drawing.Point(3616);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(5312);
            
this.label1.TabIndex = 1;
            
this.label1.Text = "最小值:";
            
// 
            // txtMin
            // 
            this.txtMin.Location = new System.Drawing.Point(9912);
            
this.txtMin.Name = "txtMin";
            
this.txtMin.Size = new System.Drawing.Size(10021);
            
this.txtMin.TabIndex = 0;
            
// 
            // label2
            // 
            this.label2.AutoSize = true;
            
this.label2.Location = new System.Drawing.Point(3615);
            
this.label2.Name = "label2";
            
this.label2.Size = new System.Drawing.Size(5312);
            
this.label2.TabIndex = 1;
            
this.label2.Text = "最小值:";
            
// 
            // txtMax
            // 
            this.txtMax.Location = new System.Drawing.Point(9951);
            
this.txtMax.Name = "txtMax";
            
this.txtMax.Size = new System.Drawing.Size(10021);
            
this.txtMax.TabIndex = 0;
            
// 
            // label3
            // 
            this.label3.AutoSize = true;
            
this.label3.Location = new System.Drawing.Point(3654);
            
this.label3.Name = "label3";
            
this.label3.Size = new System.Drawing.Size(5312);
            
this.label3.TabIndex = 1;
            
this.label3.Text = "最大值:";
            
// 
            // btnOK
            // 
            this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            
this.btnOK.Location = new System.Drawing.Point(50165);
            
this.btnOK.Name = "btnOK";
            
this.btnOK.Size = new System.Drawing.Size(7523);
            
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(173165);
            
this.button1.Name = "button1";
            
this.button1.Size = new System.Drawing.Size(7523);
            
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(284262);
            
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 { getprivate set; }

        
private void btnOK_Click(object sender, EventArgs e)
        {
            ControlProperty.Min = Convert.ToInt32(txtMin.Text);
            ControlProperty.Max = Convert.ToInt32(txtMax.Text);
        }
    }
}

posted on 2010-12-25 16:36  rolends1986  阅读(417)  评论(0编辑  收藏  举报