您的位置:首页 > 编程语言 > VB

[C#.NET][VB.NET] 自訂控制項的智能標籤-ComponentDesigner / User Control of Smart Tag-ComponentDesigner

2010-07-14 10:25 441 查看
在上篇介紹過[C#.NET][VB.NET]自訂控制項的智能標籤-ControlDesigner/UserControlofSmartTag-ControlDesigner,這有UI畫面,但萬一你設計的控制項不需要有UI畫面,你可以選擇此方案。

這篇我們需要用System.ComponentModel.Design命名空間的ComponentDesigner類別。

所以我們要將上篇的ControlDesigner改成ComponetDesigner,且PropertySmartTag類別要繼承Component類別而不是UserControl類別。





當然,繼承Component類別後,就不會有UI畫面出現,它會類似像下圖那樣。





1.建立組件控制項專案,PropertySmartTag類別繼承Component,並為類別加入屬性

publicpartialclassPropertySmartTag:Component
{
publicPropertySmartTag()
{
InitializeComponent();
}

publicPropertySmartTag(IContainercontainer)
{
container.Add(this);
InitializeComponent();
}
publicenumSexs
{
GentleMan=0,
Lady=1,
Child=2
}

privatestring_ID;
publicstringID
{
get{return_ID;}
set{_ID=value;}
}

privateSexs_Sex;
publicSexsSex
{
get{return_Sex;}
set{_Sex=value;}
}
}



2.建立CreateComponentDesigner類別,繼承System.ComponentModel.Design.ComponentDesigner。

//建立控制項設計類別
publicclassCreateComponentDesigner:System.ComponentModel.Design.ComponentDesigner
{
privateDesignerActionListCollection_ActionLists;
publicoverrideDesignerActionListCollectionActionLists
{
get
{
if(null==_ActionLists)
{
_ActionLists=newDesignerActionListCollection();
_ActionLists.Add(newCustomerActionList(this.Component));
}
return_ActionLists;
}
}
//若不想讓方法在智能面版上出現,可利用DesignerVerb定義
privatevoidOnClick2(objectsender,EventArgse)
{
MessageBox.Show("按下Click2");
}
//覆寫建構子
publicCreateComponentDesigner()
{
DesignerVerbverb=newDesignerVerb("Click2",newEventHandler(OnClick2));
this.Verbs.Add(verb);
}
}


3.建立CustomControlActionList類別,繼承System.ComponentModel.Design.DesignerActionList。

//定義智能面版樣式類別
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,Name="FullTrust")]
publicclassCustomControlActionList:System.ComponentModel.Design.DesignerActionList
{
privatePropertySmartTag_PropertySmartTag;
publicCustomControlActionList(IComponentcomponent)
:base(component)
{
_PropertySmartTag=componentasPropertySmartTag;
}

//更新自訂控制項的屬性
privatePropertyDescriptorGetPropertyByName(StringPropertyName)
{
PropertyDescriptorprop;
prop=TypeDescriptor.GetProperties(_PropertySmartTag)[PropertyName];
if(null==prop)
thrownewArgumentException("找不到此屬性名稱!",PropertyName);
else
returnprop;
}
//定義智能面版的屬性,面版屬性會與綁定的"PropertySmartTag使用者控制項"屬性同步
publicstringID
{
get{return_PropertySmartTag.ID;}
set{GetPropertyByName("ID").SetValue(_PropertySmartTag,value);}
}

publicPropertySmartTag.SexsSex
{
get{return_PropertySmartTag.Sex;}
set{GetPropertyByName("Sex").SetValue(_PropertySmartTag,value);}
}
//方法
publicvoidOnClick1()
{
MessageBox.Show("按下Click1");
}
//設計智能面版項目
publicoverrideDesignerActionItemCollectionGetSortedActionItems()
{
DesignerActionItemCollectionitems=newDesignerActionItemCollection();
//建立智能面板上項的分類。
items.Add(newDesignerActionHeaderItem("分類1","Category1"));
items.Add(newDesignerActionHeaderItem("分類2","Category2"));
items.Add(newDesignerActionHeaderItem("分類2","Category3"));
//建立智能面板上項的項目。
items.Add(newDesignerActionPropertyItem("ID","ID","Category1","選擇ID"));
items.Add(newDesignerActionPropertyItem("Sex","Sex","Category2","選擇性別"));
items.Add(newDesignerActionMethodItem(this,"OnClick1","Click1","Category3","自訂方法",true));
//items.Add(newDesignerActionMethodItem(this,"OnClick2","Click2","Category3","自訂方法",true));

items.Add(newDesignerActionTextItem("http://www.dotblogs.com.tw/yc421206","Category3"));

returnitems;
}
}


4.在PropertySmartTag類別定義Designerattribute

[Designer(typeof(CreateComponentDesigner),typeof(IDesigner))]
publicpartialclassPropertySmartTag:Component
{

}

5.加入WinForm專案,用來測試PropertySmartTag控制項



這篇範例其實並沒有寫的很完整,這當中比較重要的是當UI更新後,UI的屬性有沒有寫到控制項的屬性,有興趣的人可以練習一下怎麼傳遞變數。

下圖為專案建立步驟





範例下載

VB_UserControl_SmartTag2.zip

CS_UserControl_SmartTag2.zip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐