Group properties in a custom control

五迷三道 提交于 2019-12-04 04:04:35

问题


In our IDE, for example, Visual Studio, if we display the properties of a System.Windows.Forms.Button control, we see some properties that expose anoter set of properties. For example: FlatAppearance, Font, Location, Margin, etcetera.

I would like to do something similar in a custom control.

I know the code behind is wrong, but here is an example of what I´m trying to do:

Public Class StateOfMyCustomControl

    Public Enum EnumVisibility
        Visible
        NonVisible
    End Enum

    Public Enum EnumEventManagement
        Automatic
        Manual
    End Enum

    Private mAssociatedControl As MyCustomControl
    Private mVisibility As EnumVisibility
    Private mEventManagement As EnumEventManagement

    Public Sub New(ByVal AssociatedControl As MyCustomControl)
        mAssociatedControl = AssociatedControl
    End Sub

    Public Property Visibility() As EnumVisibility
        Get
            Return mVisibility
        End Get
        Set(ByVal value As EnumVisibility)

            mVisibility = value

            mAssociatedControl.Visible = False
            If mVisibility = EnumVisibility.Visible Then
                mAssociatedControl.Visible = True
            End If

        End Set
    End Property

    Public Property EventManagement() As EnumEventManagement
        Get
            Return mEventManagement
        End Get
        Set(ByVal value As EnumEventManagement)
            mEventManagement = value
        End Set
    End Property

End Class

Public Class MyCustomControl

    ' ...

    Private mState As StateOfMyCustomControl

    Public Sub New()
        mState = New StateOfMyCustomControl(Me)
    End Sub

    Public Property State() As StateOfMyCustomControl
        Get
            Return mState
        End Get
        Set(ByVal value As StateOfMyCustomControl)
            mState = value
        End Set
    End Property

    ' ...

End Class

In my IDE, in the properties window of my custom control, I would like to see my property State, with the possibility of display it to set the properties Visibility and EventManagement.

Thanks very much


回答1:


You need to tell it to use ExpandableObjectConverter (or a custom converter) for StateOfMyCustomControl. In C#, this is:

[TypeConverter(typeof(ExpandableObjectConverter))]
public class StateOfMyCustomControl {...}

However you apply attributes in VB, do that ;-p

Possibly:

<TypeConverter(GetType(ExpandableObjectConverter))> _
Public Class StateOfMyCustomControl
...


来源:https://stackoverflow.com/questions/755391/group-properties-in-a-custom-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!