自定义控件中的组属性

问题描述:

在我们的IDE中,例如Visual Studio,如果我们显示System.Windows.Forms.Button控件的属性,则会看到一些暴露一组属性的属性。例如:FlatAppearance,Font,Location,Margin,等等。自定义控件中的组属性

我想在自定义控件中做类似的事情。

我知道后面的代码是错误,但这里是什么我正尝试做一个例子:

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 

在我的IDE,在我的自定义控件的属性窗口,我会喜欢看我的财产状态,可以显示它来设置属性可见性EventManagement

非常感谢

你需要告诉它使用ExpandableObjectConverter(或自定义转换器),StateOfMyCustomControl。在C#中,这就是:但是你在VB应用属性

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

,做;-p

可能:

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

马克嗨,从来就进口System.ComponentModel命名空间使用TypeConverter属性。我已经证明它并且工作正常。非常感谢! – Javier 2009-04-16 15:06:13