.net WinForm用户控件开发--(6)用户控件弹出式属性设置

    这一节给大家演示下怎样使属性值以弹出式对话框的形式显示出来,先来看下效果图.

      

    这里我们定义一个用户控件,并为用户控件设置一个属性,使用弹出式对话框为属性设置值。

    定义属性ShowPropery

   代码如下

  

public partial class UCLab : UserControl
    {
        public UCLab()
        {
            InitializeComponent();
        }

        private string showpropery;
        [Description("弹出属性")]
        [Editor(typeof(ShowTypeDialogEditor),typeof(UITypeEditor))]
        public string ShowPropery
        {
            get
            {
                return showpropery;
            }
            set
            {
                showpropery = value;
            }
        }
}


   然后我们为属性设置弹出式属性编辑器,需要继承UITypeEditor类,代码如下

 

 /// <summary>
    /// 弹出式编辑器
    /// </summary>
    public class ShowTypeDialogEditor : UITypeEditor
    {

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context!=null&&context.Instance!=null)
            {
                return UITypeEditorEditStyle.Modal;//显示一个省略号
            }
            return base.GetEditStyle(context);
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            System.Windows.Forms.Design.IWindowsFormsEditorService editorService = null;
            if (context!=null&&context.Instance!=null&&provider!=null)
            {
                editorService =(System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
                if (editorService!=null)
                {
                    UCLab uclab =(UCLab)context.Instance;
                    ShowForm sf = new ShowForm(uclab.ShowPropery);
                    if (sf.ShowDialog()==DialogResult.OK)
                    {
                           value = sf.Result;
                            return value;
                    }
                }
            }
            //return base.EditValue(context, provider, value);
                return value;
        }
    }


   这样我们把用户控件拖到界面上,就可以设置属性了。

 

   


版权声明:本文为zx13525079024原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。