熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

.net WinForm用戶控件開發:用戶控件彈出式屬性設置

2013-11-15 12:50:18  來源: ASP編程 

  這一節給大家演示下怎樣使屬性值以彈出式對話框的形式顯示出來先來看下效果圖

  

  這裡我們定義一個用戶控件並為用戶控件設置一個屬性使用彈出式對話框為屬性設置值

  定義屬性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&&contextInstance!=null) { return UITypeEditorEditStyleModal;//顯示一個省略號 } return baseGetEditStyle(context); } public override object EditValue(ITypeDescriptorContext context IServiceProvider provider object value) { SystemWindowsFormsDesignIWindowsFormsEditorService editorService = null; if (context!=null&&contextInstance!=null&&provider!=null) { editorService =(SystemWindowsFormsDesignIWindowsFormsEditorService)providerGetService(typeof(SystemWindowsFormsDesignIWindowsFormsEditorService)); if (editorService!=null) { UCLab uclab =(UCLab)contextInstance; ShowForm sf = new ShowForm(uclabShowPropery); if (sfShowDialog()==DialogResultOK) { value = sfResult; return value; } } } //return baseEditValue(context provider value); return value; } }

  這樣我們把用戶控件拖到界面上就可以設置屬性了

  


From:http://tw.wingwit.com/Article/program/ASP/201311/21730.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.