C#組件編程(1)

布满荆棘的人生 2022-08-26 03:45 244阅读 0赞

C#組件編程(1)

1.Component与Control之间的区别
(1)Component在Run Time时不能呈现UI,而Control可以在Run Time时呈现UI(但是vs 2005里的asp.net中的SqlDataSource是Control,但是它不能呈现UI)。
(2)Component是贴在容器Container上的,而Control则是贴在Windows Form或者Web Form上的。
举例来说,SqlCommand是个Component,DataGrid则是一个Control。

2.Property与Attribute的區別
在中文中這兩個是沒有區別的,本文將從字面上給以區別:Property表示屬性,Attribute表示特性.

3.一个简单的Component
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Components
{
public class Component1 : Component
{
private int _id;
private string _name;
private DateTime _createDateTime;

// 在Property窗口中为灰色显示。
public int Id
{
get { return _id; }
}

// 在Property窗口中可以设置值。
public string name
{
get { return _name; }
set { _name = value; }
}

// 在Property窗口中不可见。
public DateTime CreateDateTime
{
set { _createDateTime = value; }
}
}
}

4.各种Attribute
--1.Attribute列表
EventAttribute有:
BrowsableAttribute 、CategoryAttribute、DescriptionAttribute、DefaultEventAttribute

PropertyAttribute有:
BrowsableAttribute 、CategoryAttribute、DescriptionAttribute、DefaultPropertyAttribute、ReadOnlyAttribute、 DefaultValueAttribute、EditorAttribute
、DesignerSerializationVisibilityAttribute、TypeConverterAttribute、BindableAttribute、LocalizableAttribute

MethodAttribute有:
WebMethod

--2.Attribute用途:
BrowsableAttribute:在Property窗口中是否可见。
CategoryAttribute:Property或者Event所属的哪个组。
DescriptionAttribute:Property或者Event的简单描述。
ReadOnlyAttribute : Property在屬性視窗中不可改寫
DefaultEventAttribute:默认Event、选中组件,其Event窗口中默认选中在这个Event上。
DefaultPropertyAttribute:默认Property,选中组件,其Property窗口中默认选中在这个Property上。
DefaultValueAttribute:Property的默认值
EditorAttribute:指定Property Editor使用的编辑器。
DesignerSerializationVisibilityAttribute:指定通过Property Editor得到的结果是否保存在代码中。
LocalizableAttribute:用户要本地化某个窗体时,任何具有该特性的属性都将自动永久驻留到资源文件中。

--3.使用示例:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Components
{
// PropertyAttribute、EventAttribute分别放在Property、Event上,并用[]括起来。
// DefaultPropertyAttribute、DefaultEventAttribute必须放在类头上。
[DefaultEvent(“CustomerLogout”)]
public class Customer : Component
{
private string _id;
private string _sex;
private int _age=20;
private string _address;
private DateTime _createTime;

// 没有CategoryAttribute、DescriptionAttribute。
public string Id
{
get { return _id; }
set { _id = value; }
}

// 此属性在Customer’s Details分组中,CategoryAttribute、DescriptionAttribute也适用于Event。
[Category(“Customer’s Details”), Description(“Customer’s Sex”)] // 可以在一个[]里写两个Attribute。
public string Sex
{
get { return _sex; }
set { _sex = value; }
}

//屬性默認值為20
[Category(“Customer’s Details”)]
[Description(“Customer’s Age”), DefaultValue(20)]
public int Age
{
get { return _age; }
set { _age = value; }
}

[DefaultValue(“shanghai”),Category(“Customer’s Details”)]
public string Address
{
get { return _address; }
set { _address = value; }
}

// 此Property在Property窗口中不可见,BrowsableAttribute也适用于Event。
[Browsable(false)]
public DateTime CreateTime
{
get { return _createTime; }
set { _createTime = value; }
}

public sealed class CustomerLoginEventArgs : EventArgs
{ }
public sealed class CustomerLogoutEventArgs : EventArgs
{ }

public delegate void CustomerLoginEventHandler(object sender, CustomerLoginEventArgs e);
public delegate void CustomerLogoutEventHandler(object sender, CustomerLogoutEventArgs e);

public event CustomerLoginEventHandler CustomerLogin
{
add { }
remove { }
}

public event CustomerLogoutEventHandler CustomerLogout
{
add { }
remove { }
}
}
}

发表评论

表情:
评论列表 (有 0 条评论,244人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C#(2)

    C\組件編程(2) 較复雜的屬性為了編輯方便,就要用到屬性編輯器. 由于在屬性視窗中只能識別字符串類型,如果是非字符串類型的屬性還需要用到類型轉換器. 下面就分別講它

    相关 Socket--同步的應用

    同步編程使用的情況不多,在以下3種情況下可以使用同步: 1.客戶端數量較少情況下的服務端編程 2.客戶端數量較多,但都是短連接情況下的服務端編程 3.客戶端編程

    相关 C#中的碼轉化

    C\中的編碼轉化 在轉化之前,必須要先知道當前變量的編碼.對於.NET下的string永远是Unicode的. 對於從文本文件中讀取的byte\[\]或string的編碼,也

    相关 JS規範 積累

    1.命名推薦:userName(第一個單詞首字母小寫,其他首字母大寫) 2.函數名有必要加上前綴fn,比如fnGetName,最好採用動名詞組合,函數的參數要加註釋 3