九月 16

PropertyGrid 這個元件在市面上 .net  的書很少看到

這幾天摸了一下,感覺上是個還不錯用的元件

尤其對於在開發"編輯環境"的特別好用

簡單的說,這是一個可以幫你把Class變成設定介面的元件

把Class丟進元件後,元件就會幫你處理傳值與顯示值的問題

看圖或許比較好理解

有沒有一種很熟悉的感覺,沒錯,跟 Visual Studio .Net 的的屬性設定是一樣的

而這個介面只要把寫好的Class放進去就可以自動產生了,真的超方便

完整的程式碼可以到這邊下載

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        '把Class丟進去PropertyGrid
        PropertyGrid1.SelectedObject = New SampleClass
    End Sub

SampleClass.vb

Imports System.ComponentModel

Public Class SampleClass

    <FlagsAttribute()> _
   Public Enum Permissions
        <Description("未設定")> _
        None = 0

        <Description("建立")> _
        Create = 1

        <Description("讀取")> _
        Read = 2

        <Description("更新")> _
        Update = 4

        <Description("刪除")> _
        Delete = 8

        <Description("所有功能")> _
        All = Create Or Read Or Update Or Delete

    End Enum

    Private _P1 As String
    Public Property P1_String() As String
        Get
            Return _P1
        End Get
        Set(ByVal value As String)
            _P1 = value
        End Set
    End Property

    Private _P2 As Integer
    Public Property P2_Integer() As Integer
        Get
            Return _P2
        End Get
        Set(ByVal value As Integer)
            _P2 = value
        End Set
    End Property

    Private _P3 As Boolean
    Public Property P3_Boolen() As Boolean
        Get
            Return _P3
        End Get
        Set(ByVal value As Boolean)
            _P3 = value
        End Set
    End Property

    Private _P4 As DateTime
    Public Property P4_DateTime() As DateTime
        Get
            Return _P4
        End Get
        Set(ByVal value As DateTime)
            _P4 = value
        End Set
    End Property

    Private _P5 As Permissions
    Public Property P5_Enum() As Permissions
        Get
            Return _P5
        End Get
        Set(ByVal value As Permissions)
            _P5 = value
        End Set
    End Property

    Private _P6 As New PropertyTreeClass.AddressType
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    <Description("住址")> _
    Public Property P6_Tree() As PropertyTreeClass.AddressType
        Get
            Return _P6
        End Get
        Set(ByVal value As PropertyTreeClass.AddressType)
            _P6 = value
        End Set
    End Property

    Private _P7 As Color = Color.White
    Public Property P7_Color() As Color
        Get
            Return _P7
        End Get
        Set(ByVal value As Color)
            _P7 = value
        End Set
    End Property

    Private _P8 As Color = System.Drawing.Color.FromArgb(123, 123, 123)
    <Description("於自訂項目的下面空格中按下滑鼠右鍵,可自訂RGB")> _
    Public Property P8_Color() As Color
        Get
            Return _P8
        End Get
        Set(ByVal value As Color)
            _P8 = value
        End Set
    End Property

#Region "可分類的屬性"
    Private _C1 As String
    <Category("分類")> _
    <Description("相同類型的屬性C1")> _
    Public Property C1() As String
        Get
            Return _C1
        End Get
        Set(ByVal value As String)
            _C1 = value
        End Set
    End Property

    Private _C2 As String
    <Category("分類")> _
    <Description("相同類型的屬性C2")> _
    Public Property C2() As String
        Get
            Return _C2
        End Get
        Set(ByVal value As String)
            _C2 = value
        End Set
    End Property
#End Region

End Class

PropertyTreeClass.vb

Imports System.ComponentModel
Imports System.Globalization

'The namespace referenced from
'http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/f81b6caa-5fae-45c4-ad46-2240e84a5d7a

Namespace PropertyTreeClass

    <TypeConverter(GetType(AddressTypeTypeConverter))> _
    Public Class AddressType

        Private m_country As String
        <NotifyParentProperty(True), Description("國家"), RefreshProperties(RefreshProperties.Repaint)> _
        Public Property Country() As String
            Get
                Return m_country
            End Get

            Set(ByVal value As String)
                m_country = value
            End Set
        End Property

        Private m_city As String
        <NotifyParentProperty(True), Description("城市"), RefreshProperties(RefreshProperties.Repaint)> _
        Public Property City() As String
            Get
                Return m_city
            End Get

            Set(ByVal value As String)
                m_city = value
            End Set
        End Property
    End Class

    Public Class Person

        Public Sub New()
            homeAddressField = New AddressType()
        End Sub

        Private homeAddressField As AddressType

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public Property HomeAddress() As AddressType
            Get
                Return Me.homeAddressField
            End Get

            Set(ByVal value As AddressType)
                Me.homeAddressField = value
            End Set
        End Property
    End Class

    Public Class AddressTypeTypeConverter
        Inherits TypeConverter

        Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
            'This method is used to shown information in the PropertyGrid.
            If destinationType Is GetType(String) Then
                Return (DirectCast(value, AddressType).Country & ",") + DirectCast(value, AddressType).City
            End If

            Return MyBase.ConvertTo(context, culture, value, destinationType)
        End Function

        Public Overrides Function GetProperties(ByVal context As ITypeDescriptorContext, ByVal value As Object, ByVal attributes As Attribute()) As PropertyDescriptorCollection
            Return TypeDescriptor.GetProperties(GetType(AddressType), attributes).Sort(New String() {"Country", "City"})
        End Function

        Public Overrides Function GetPropertiesSupported(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function

    End Class

End Namespace