using System;
using System.ComponentModel;
using System.Windows.Forms;namespace PropertyStudy
{
/// <summary>
/// MyPannel 的摘要说明。
/// </summary>
public class MyPannel : System.Windows.Forms.Panel
{
public MyPannel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private string des; [DefaultValueAttribute("Jason")]
[Category("微观")]
[Description("一个简单的描述")]
public string Des
{
get{return this.des;}
set{this.des = value;}
} private string filename;
/// <summary>
/// 下拉菜单的形式
/// </summary>
[CategoryAttribute("自定义的复杂类型设置(包括自定义类型转换器)"),
TypeConverterAttribute(typeof(PropertyStudy.FileNameConvertor)),
ReadOnlyAttribute(false)]
public string FileName
{
get{return this.filename;}
set{this.filename = value;}
}
private ExpandProperty _dropList = new ExpandProperty();
/// <summary>
/// 展开形式的属性
/// </summary>
[CategoryAttribute("自定义的复杂类型设置(包括自定义类型转换器)"),
TypeConverterAttribute(typeof(PropertyStudy.ExpandConverter)),
ReadOnlyAttribute(false)]
public ExpandProperty DropList
{
get { return this._dropList;}
set { this._dropList= value;}
}
private string _appVer="1.0"; 
/// <summary>
/// 弹出对话框的形式
/// </summary>
[CategoryAttribute("自定义编辑器"),
DefaultValueAttribute("1.0"),
DescriptionAttribute("版本信息"),
ReadOnlyAttribute(true),
EditorAttribute(typeof(AppVerConverter),typeof(System.Drawing.Design.UITypeEditor))]
public string AppVer
{
get {return this._appVer;}
set {this._appVer=value;}
}
private System.Drawing.Point _dropUI;
/// <summary>
/// 下拉UI
/// </summary>
[CategoryAttribute("自定义编辑器"),
DefaultValueAttribute("1"),
DescriptionAttribute("下拉可视控件"),
ReadOnlyAttribute(false),
EditorAttribute(typeof(DropEditor),typeof(System.Drawing.Design.UITypeEditor))]
public System.Drawing.Point DropUI
{
get { return this._dropUI;}
set { this._dropUI=value; }
}
}

解决方案 »

  1.   

    /// <summary>
    /// 扩展字符串的转换器(实现下拉列表框的样式)
    /// </summary>
    public class FileNameConvertor : System.ComponentModel.StringConverter
    {
    /// <summary>
    /// 根据返回值确定是否支持下拉框的形式
    /// </summary>
    /// <returns>
    /// true: 下来框的形式
    /// false: 普通文本编辑的形式
    /// </returns>
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
    return true;
    }

    /// <summary>
    /// 下拉框中具体的内容
    /// </summary>
    public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)
    {
    return new StandardValuesCollection(new string[]{"File1.bat","File2.exe","File3.dll"});
    } /// <summary>
    /// 根据返回值确定是否是不可编辑的文本框
    /// </summary>
    /// <returns>
    /// true:  文本框不可以编辑
    /// flase: 文本框可以编辑
    /// </returns>
    public override bool GetStandardValuesExclusive(System.ComponentModel.ITypeDescriptorContext context)
    {
    return true;
    }
    }
    /// <summary>
    /// 用来进行展开的形式演示
    /// </summary>
    public class ExpandProperty
    {
    private int _intList=0; public int IntList
    {
    get { return this._intList;}
    set { this._intList=value; }
    } private string _strList="Null"; public string StrList
    {
    get { return this._strList;}
    set { this._strList= value;}
    }
    }
      

  2.   

    /// <summary>
    ///  可以展开的类型转换器
    ///     ExpandProperty
    /// </summary>
    public class ExpandConverter:System.ComponentModel.ExpandableObjectConverter
    {
    public ExpandConverter()
    {
    } /// <summary>
    /// 覆盖此方法已确定属性是否可以转换
    /// </summary>
    public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType)
    {
    if (destinationType==typeof(PropertyStudy.ExpandProperty))
    return true;
    return base.CanConvertTo(context,destinationType);
    } /// <summary>
    /// 覆盖此方法并确保destinationType参数是一个String,然后格式化所显示的内容
    /// </summary>
    public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
    {
    if (destinationType == typeof (System.String) && value is PropertyStudy.ExpandProperty)
    {
    PropertyStudy.ExpandProperty source=(PropertyStudy.ExpandProperty)value;
    return source.IntList+","+source.StrList;
    }
    return base.ConvertTo(context,culture,value,destinationType);
      } /// <summary>
    /// 覆盖此方法已确定输入的字符串是可以被转化
    /// </summary>
    public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
    {
    if (sourceType==typeof(string))
    return true;
    return base.CanConvertFrom(context,sourceType);
    } /// <summary>
    /// 覆盖此方法根据 ConvertTo() 方法的转换格式来把所输入的字符串转换成类,并返回该类
    /// </summary>
    public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
    if (value is string)
    {
    string s=(string)value;
    int comma=s.IndexOf(",");
    if (comma!=-1)
    {
    try
    {
    string intList=s.Substring(0,comma);
    string strList=s.Substring(comma+1,s.Length-comma-1);
    PropertyStudy.ExpandProperty Ep=new ExpandProperty();
    Ep.IntList=int.Parse(intList);
    Ep.StrList=strList;
    return Ep;
    }
    catch
    {
    return base.ConvertFrom(context,culture,value);
    }
    }
    }
    return base.ConvertFrom(context,culture,value);
    }
    } /// <summary>
    /// 自定义UI的属性编辑器(弹出消息)
    /// </summary>
    public class AppVerConverter:System.Drawing.Design.UITypeEditor
    {
    /// <summary>
    /// 覆盖此方法以返回编辑器的类型。
    /// </summary>
    public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
    {
    return System.Drawing.Design.UITypeEditorEditStyle.Modal;
    } /// <summary>
    /// 覆盖此方法以显示版本信息
    /// </summary>
    public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
    System.Windows.Forms.MessageBox.Show("版本:1.0\n作者:张翔","版本信息");
    return value;
    }
    }
    public class DropEditor:System.Drawing.Design.UITypeEditor
    {
    public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
    {
    return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
    } public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
    System.Windows.Forms.Design.IWindowsFormsEditorService iws=(System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
    if (iws!=null)
    {
    PropertyStudy.DropUIControl UIControl=new PropertyStudy.DropUIControl((System.Drawing.Point)value,iws);
    iws.DropDownControl(UIControl);
    return UIControl.Value;
    }
    return value;
    }
    } internal class DropUIControl:System.Windows.Forms.UserControl
    {
    public DropUIControl(System.Drawing.Point avalue,System.Windows.Forms.Design.IWindowsFormsEditorService iws)
    {
    this.Value=avalue;
    this._tmpvalue=avalue;
    this._iws=iws;
    this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer|System.Windows.Forms.ControlStyles.UserPaint|System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true);
    this.BackColor=System.Drawing.SystemColors.Control;
    }
    private System.Drawing.Point _value; public System.Drawing.Point Value
    {
    get { return this._value;}
    set { this._value=value; }

    private System.Drawing.Point _tmpvalue;
    private System.Windows.Forms.Design.IWindowsFormsEditorService _iws; protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
    string str="X:"+this._tmpvalue.X.ToString()+"  ;Y:"+this._tmpvalue.Y.ToString();
    System.Drawing.Graphics g=e.Graphics;
    System.Drawing.SizeF sizef= g.MeasureString(str,this.Font);
    g.DrawString(str,this.Font,
    new System.Drawing.SolidBrush(System.Drawing.Color.Black),
    (int)((this.Width-(int)sizef.Width)/2),
    this.Height-(int)sizef.Height);
    g.PageUnit=System.Drawing.GraphicsUnit.Pixel;
    g.FillEllipse(new System.Drawing.SolidBrush(System.Drawing.Color.Red),
    this.Value.X-2,this.Value.Y-2,4,4);
    } protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
    {
    base.OnMouseMove(e);
    this._tmpvalue=new System.Drawing.Point(e.X,e.Y);
    this.Invalidate();
    } protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
    {
    base.OnMouseUp(e);
    this.Value=this._tmpvalue;
    this.Invalidate();
    if (e.Button==System.Windows.Forms.MouseButtons.Left)
    this._iws.CloseDropDown();
    }
    }
    }
      

  3.   

    万分感谢“ brightheroes(闭关|学习ASP.NET中~) ”
    佩服佩服!!!
      

  4.   

    请参考:
    http://www.csdn.net/develop/Read_Article.asp?id=24936
    http://www.csdn.net/develop/Read_Article.asp?id=24937
    http://www.csdn.net/develop/Read_Article.asp?id=24938
    http://www.csdn.net/develop/Read_Article.asp?id=24939经典文章收藏-------自定义属性的性质