[C#.NET][VB.NET] 自訂控制項的顯示視窗屬性 /User Control of Properties(二)
2010-07-16 08:19
1716 查看
續上篇自訂控制項的顯示視窗屬性/UserControlofProperties(二),為節省篇幅,VB語法就不列在上面了,需要的人再去下載範例。
顯示複雜屬性
我們可以利用.NET本身所提供的類別拿來當傳遞
字型
實體化一個預設字型。
顏色
尺寸
圖形
看了以上幾個例子後,我們就可以知道屬性也可以把類別丟來丟去的。
自訂動態下拉清單
enum可以達到下拉清單的功能,但萬一我們需要的清單是動態的,就沒辦法使用enum了,這時我們可以這樣做:
1.建立繼承StringConverter類別,並複寫
1-1覆寫GetStandardValuesSupported方法並傳回true,表示能從清單挑選值。
1-2覆寫GetStandardValues方法,並傳回填入您的標準值的StandardValuesCollection。表示建立清單選擇。
1-3覆寫GetStandardValuesExclusive方法並傳回false。讓使用者能輸入下拉式清單以外的值。
2.建立屬性
回傳自訂類別
當自己開發一個類別後要怎麼在屬性視窗裡顯示?
參考http://dotnetframework.blogspot.com/2007/10/blog-post.html
1.首先先建立一個類別,並定義RefreshProperties屬性。
此處的RefreshProperties屬性主要是用來自動更新其它屬性
2.建立一繼承ExpandableObjectConverter個類別,並覆寫
CanConvertTo:傳回型別轉換子是否能將物件轉換成指定的型別。
ConvertTo:用來存放資訊,將指定的值物件轉換為指定的型別,如下圖:
CanConvertFrom:(選擇性)可以指定型別轉換器可以從字串轉換,以便在文字方塊中編輯物件的字串,如下圖:
ConvertFrom:從指定的值轉換成此轉換子的預期轉換型別。
3.套用CustomClassConverter類別,加入TypeConverterAttribute屬性
4.在自訂控制項中引用自訂類別
範例下載
UserControls-1.zip
顯示複雜屬性
我們可以利用.NET本身所提供的類別拿來當傳遞
字型
實體化一個預設字型。
privateFont_CustomFont=newFont("新細明體",12,FontStyle.Regular); [Category("自訂屬性"),Description("字型大小")] publicFontCustomFont { get{return_CustomFont;} set{_CustomFont=value;} }
顏色
privateColor_CustombarColor=SystemColors.Control; [Category("自訂屬性"),Description("控制項顏色")] publicColorCustombarColor { get{return_CustombarColor;} set{_CustombarColor=value;} }
尺寸
privateSize_CustomSize=newSize(100,100); [Category("自訂屬性"),Description("視窗大小")] publicSizeCustomSize { get{return_CustomSize;} set{_CustomSize=value;} }
圖形
[Category("自訂屬性"),Description("視窗大小")] publicImageCustomImage{get;set;}
看了以上幾個例子後,我們就可以知道屬性也可以把類別丟來丟去的。
自訂動態下拉清單
enum可以達到下拉清單的功能,但萬一我們需要的清單是動態的,就沒辦法使用enum了,這時我們可以這樣做:
1.建立繼承StringConverter類別,並複寫
1-1覆寫GetStandardValuesSupported方法並傳回true,表示能從清單挑選值。
1-2覆寫GetStandardValues方法,並傳回填入您的標準值的StandardValuesCollection。表示建立清單選擇。
1-3覆寫GetStandardValuesExclusive方法並傳回false。讓使用者能輸入下拉式清單以外的值。
publicclassNameConverter:StringConverter { publicoverrideboolGetStandardValuesSupported(ITypeDescriptorContextcontext) { returntrue; } publicoverrideStandardValuesCollectionGetStandardValues(ITypeDescriptorContextcontext) { returnnewStandardValuesCollection(newstring[]{"余小章","張大呆","吳小明"}); } //(選擇性)讓使用者能輸入下拉式清單以外的值 publicoverrideboolGetStandardValuesExclusive(ITypeDescriptorContextcontext) { returnfalse; } }
2.建立屬性
publicpartialclassCSUserControl1:UserControl { publicCSUserControl1() { InitializeComponent(); } privatestring_DefaultFileName="余小章"; [TypeConverter(typeof(NameConverter)),CategoryAttribute("自訂屬性"),Description("自訂清單")] publicstringDefaultFileName { get{return_DefaultFileName;} set{_DefaultFileName=value;} } }
回傳自訂類別
當自己開發一個類別後要怎麼在屬性視窗裡顯示?
參考
1.首先先建立一個類別,並定義RefreshProperties屬性。
publicclassCustomClass { privatestring_CustomName="余小章"; [Category("自訂屬性"),Description("自訂名稱"),RefreshProperties(RefreshProperties.All)] publicstringCustomName { get{return_CustomName;} set{_CustomName=value;} } privateint_CustomAge=19; [Category("自訂屬性"),Description("自訂年齡"),RefreshProperties(RefreshProperties.All)] publicintCustomAge { get{return_CustomAge;} set{_CustomAge=value;} } }
此處的RefreshProperties屬性主要是用來自動更新其它屬性
2.建立一繼承ExpandableObjectConverter個類別,並覆寫
CanConvertTo:傳回型別轉換子是否能將物件轉換成指定的型別。
ConvertTo:用來存放資訊,將指定的值物件轉換為指定的型別,如下圖:
CanConvertFrom:(選擇性)可以指定型別轉換器可以從字串轉換,以便在文字方塊中編輯物件的字串,如下圖:
ConvertFrom:從指定的值轉換成此轉換子的預期轉換型別。
publicclassCustomClassConverter:ExpandableObjectConverter
{
publicoverrideboolCanConvertTo(ITypeDescriptorContextcontext,System.TypedestinationType)
{
if(destinationType==typeof(CustomClass))
returntrue;
returnbase.CanConvertTo(context,destinationType);
}
publicoverrideobjectConvertTo(ITypeDescriptorContextcontext,CultureInfoculture,objectvalue,System.TypedestinationType)
{
if(destinationType==typeof(System.String)&&valueisCustomClass)
{
CustomClassso=(CustomClass)value;
return"名字:"+so.CustomName+","+
"年齡為:"+so.CustomAge;
}
returnbase.ConvertTo(context,culture,value,destinationType);
}
//(選擇性)您可以指定型別轉換器可以從字串轉換,以便在文字方塊中編輯物件的字串
publicoverrideboolCanConvertFrom(ITypeDescriptorContextcontext,System.TypesourceType)
{
if(sourceType==typeof(string))
returntrue;
returnbase.CanConvertFrom(context,sourceType);
}
publicoverrideobjectConvertFrom(ITypeDescriptorContextcontext,CultureInfoculture,objectvalue)
{
if(valueisstring)
{
try
{
stringstr=(string)value;
intcolon=str.IndexOf(':');
intcomma=str.IndexOf(',');
if(colon!=-1&&comma!=-1)
{
stringname=str.Substring(colon+1,(comma-colon-1));
colon=str.IndexOf(':',comma+1);
stringage=str.Substring(colon+1);
CustomClassso=newCustomClass();
so.CustomAge=Int16.Parse(age);
so.CustomName=name;
returnso;
}
}
catch
{
thrownewArgumentException("無法將'"+(string)value+"'轉換為CustomClass型別");
}
}
returnbase.ConvertFrom(context,culture,value);
}
}
3.套用CustomClassConverter類別,加入TypeConverterAttribute屬性
[TypeConverterAttribute(typeof(CustomClassConverter))]
publicclassCustomClass
{
}
4.在自訂控制項中引用自訂類別
privateCustomClass_CustomClass=newCustomClass();
[Category("自訂屬性"),Description("自訂類別")]
publicCustomClassCustomClass
{
get{return_CustomClass;}
set{_CustomClass=value;}
}
範例下載
相关文章推荐
- [C#.NET][VB.NET] 自訂控制項的顯示視窗屬性 /User Control of Properties(一)
- [C#.NET][VB.NET] 自訂控制項的自訂屬性編輯視窗 / User Control of Custom Properties Editor
- [C#.NET][VB.NET] 自訂控制項的集合屬性 /User Control of Collection Properties
- [C#.NET][VB.NET] 自訂控制項的自訂集合屬性 /User Control of Custom Collection Properties
- [C#.NET][VB.NET] 自訂控制項的智能標籤-ControlDesigner / User Control of Smart Tag-ControlDesigner
- [C#.NET][VB.NET] 自訂控制項的智能標籤-ComponentDesigner / User Control of Smart Tag-ComponentDesigner
- The step-by-step guide of making a C# (or VB.NET) Windows 7 Trigger Start Service
- VB.net对于datagride控件的使用及C#中datagrideView\bindingSource\bindingNavigator\tabcontrol控件的应用
- c# 自訂控制項的顯示視窗屬性
- 获取本机已安装的软件列表C#/VB.NET(Get the list of installed software in local pc)
- 【转】[C#.NET][VB.NET] 用 Win32 API 控制 外部應用程式選單
- [C#.NET][VB.NET] Winform 應用程式等級的例外捕捉 / Winform Application of Level wicth Exception Catch
- (.NET) IntelliSense difference of Extension Method name in Statement Completion for VB and C#.
- vb.net C# AtlAxGetControl 函数使用方法
- Create a ActiveX user control (VB.NET2005)
- VB.NET/C# Free Grid Control 免费开源表格控件 - ReoGrid 介绍(1)
- VisualC#.NET编写服务器日期控件并使用(UserControl and CustomControl)
- [C#.NET][VB.NET] Winform 應用程式等級的例外捕捉 / Winform of Application Level wicth Exception Catch(转载)
- [C#.NET][VB.NET] 程式執行時拖曳控制項
- c# vb.net 的委托