控制項學習九(AttributeCollection及工具箱圖示)

 

十一.屬性設計的進階技巧

1.當屬性是指向某個集合或物件

當我們使用某些控制項的時候,屬性似乎不是屬性,而是一個另外一個物件。看下面的例子。

我們在頁面上布置一個TextBox,你會發現可以這樣用”me.TextBox1.Attributes.Add()”

其實,Attributes是它的一個屬性。單純的屬性是不會有方法和其他屬性的,很顯然,Attributes是指向另外一個結構——AttributeCollection物件。

Imports System.ComponentModel

Imports System.Web.UI

 

<DefaultProperty("items"), ToolboxData("<{0}:MyTestControlA runat=server></{0}:MyTestControlA>")> Public Class MyTestControlA

    Inherits System.Web.UI.WebControls.WebControl

    '儲存一個項目的類別

    Class DataItem

        Public data1 As String

        Sub New(ByVal data)

            data1 = data

        End Sub

    End Class

    '定義項目集合的類別(給屬性用)

    Class DataItems

        Inherits CollectionBase

        Default Public Property item(ByVal index As Integer) As DataItem

            Get

                Return CType(List(index), DataItem)

            End Get

            Set(ByVal Value As DataItem)

                List(index) = Value

            End Set

        End Property

        Public Sub Remove(ByVal value As DataItem)

            List.Remove(value)

        End Sub 'Remove

        Public Function Add(ByVal value As DataItem)

            Return List.Add(value)

        End Function 'Add

    End Class

    'DataItems

    Dim _items As New DataItems

    '指向 DataItems 的屬性

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property items() As DataItems

        Get

            Return _items

        End Get

        Set(ByVal Value As DataItems)

            _items = Value

        End Set

    End Property

    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)

        output.RenderBeginTag("div")

        output.Write("<br>MyTestControls<br>")

        If Not (items Is Nothing) Then

            '顯示 items 屬性中的每一個元素

            For Each i As DataItem In Me.items()

                output.Write("<br>" & i.data1)

            Next

        End If

        output.RenderEndTag()

    End Sub

End Class

.NET Framework為了處理這種十分常用的需求,設計了CollectionBase,作為抽象的基底類別(Base Class)。

3.指向陣列(集合)的預設屬性

我們曾經提過,控制項的事件有預設事件,控制項的屬性也有預設屬性,要將屬性設為屬性相當簡單,只需要在宣告屬性的時候加上【Default】關鍵字即可,唯一一個條件,是該屬性必須有傳入值例如:

        Default Public Property item(ByVal index As Integer) As DataItem

            Get

                Return CType(List(index), DataItem)

            End Get

            Set(ByVal Value As DataItem)

                List(index) = Value

            End Set

        End Property

之後,我們就可以這樣用Me.MyTestControlA1.items.item(0).data1=”abc”
 

十二.控制項的工具箱圖示

當我們建置控制項的時候,請加入一個“同名的”.bmp圖檔,這個圖檔是16X16的大小,建置動作必須為“內嵌資源”。

转载于:https://www.cnblogs.com/htht66/archive/2008/11/15/1334185.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C# 中使用 CAD2019 二次开发获取块属性,可以按照以下步骤进行: 1. 获取块表记录(BlockTableRecord)对象,可以通过以下代码获取: ``` // 获取当前文档的数据库 Database db = HostApplicationServices.WorkingDatabase; // 开启事务处理 using (Transaction trans = db.TransactionManager.StartTransaction()) { // 获取块表 BlockTable blkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; // 获取块表记录 BlockTableRecord blkTblRec = trans.GetObject(blkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord; // 进行其他操作 // ... } ``` 2. 在获取到块表记录对象后,可以通过块表记录的方法 GetBlockReferenceIds() 获取到块参照的 ID 列表,然后遍历 ID 列表,获取每个块参照对象的属性值,代码如下: ``` // 遍历块参照对象 foreach (ObjectId blkRefId in blkTblRec.GetBlockReferenceIds(true, true)) { // 获取块参照对象 BlockReference blkRef = trans.GetObject(blkRefId, OpenMode.ForRead) as BlockReference; // 获取块参照对象的属性集合 AttributeCollection attCol = blkRef.AttributeCollection; // 遍历属性集合 foreach (ObjectId attId in attCol) { // 获取属性对象 DBObject dbObj = trans.GetObject(attId, OpenMode.ForRead); // 判断是否为属性对象 AttributeReference attRef = dbObj as AttributeReference; if (attRef != null) { // 获取属性名和属性值 string attName = attRef.Tag; string attValue = attRef.TextString; // 进行其他操作 // ... } } } ``` 通过以上代码,即可获取块参照对象的属性值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值