Property in PropertyGrid disabled

06/02/2008 - 08:34 von Robert Klein | Report spam
Hallo Ng,

ich habe folgendes Problem:
Ich zu Beispielzwecken die Klasse Windows.Form.Timer überschrieben (CTimer)
und dieser Klasse ein Property (Props) hinzugefügt, das eine Sammlung von
Eigenschaften einer "Unterklasse" repràsentiert. Wenn ich nun die Klasse
CTimer auf ein Form ziehe, erscheint das hinzugefügte Property "Props" im
PropertGrid nur disabled.

Hab mit den Attributen Browsable, Editor, etc. schon herumgespielt, ich
kriegs aber nicht hin.
Kann mir jemand helfen. Dankeschön schon mal im Voraus.
Gruß Robert


Hier der BeispielCode:

'**********************************************************************
' CTimer
'**********************************************************************
Public Class CTimer
Inherits System.Windows.Forms.Timer

'**************************************
' Props
'**************************************
Private m_Props As CTimerProps
Public Property Props() As CTimerProps
Get
Return m_Props
End Get
Set(ByVal value As CTimerProps)
m_Props = value
End Set
End Property 'Props

'**************************************
' New
'**************************************
Public Sub New()
m_Props = New CTimerProps
End Sub 'New
End Class 'CTimer

'**********************************************************************
' CTimerProps
'**********************************************************************
Public Class CTimerProps

'**************************************
' Index
'**************************************
Private m_Index As Int32
Public Property Index() As Int32
Get
Return m_Index
End Get
Set(ByVal value As Int32)
m_Index = value
End Set
End Property 'Index

'**************************************
' ID
'**************************************
Private m_ID As String
Public Property ID() As String
Get
Return m_ID
End Get
Set(ByVal value As String)
m_ID = value
End Set
End Property 'ID

'**************************************
' New
'**************************************
Public Sub New()
m_Index = 1
m_ID = Guid.NewGuid.ToString("N")
End Sub 'New
 

Lesen sie die antworten

#1 Karsten Sosna
06/02/2008 - 17:20 | Warnen spam
ich habe folgendes Problem:
Ich zu Beispielzwecken die Klasse Windows.Form.Timer überschrieben
(CTimer)
und dieser Klasse ein Property (Props) hinzugefügt, das eine Sammlung von
Eigenschaften einer "Unterklasse" repràsentiert. Wenn ich nun die Klasse
CTimer auf ein Form ziehe, erscheint das hinzugefügte Property "Props" im
PropertGrid nur disabled.

Hab mit den Attributen Browsable, Editor, etc. schon herumgespielt, ich
kriegs aber nicht hin.
Kann mir jemand helfen. Dankeschön schon mal im Voraus.



Hallo Robert,
dazu brauchst Du einen TypeConverter(hier ein ExpandableObjectConverter).
Hier ein Beispiel. Formatierung spare ich mir. ;=)
Gruß Scotty

\\\
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Globalization
Imports System.Reflection
#Region "PointXYZ Structure"
<TypeConverter(GetType(PointXYZConverter))> _
Public Structure PointXYZ
' Leere Instanz
Public Shared ReadOnly Empty As PointXYZ
Private _X As Integer
Private _Y As Integer
Private _Z As Integer
Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer)
Me._X = x
Me._Y = y
Me._Z = z
End Sub
<DefaultValueAttribute(0), _
CategoryAttribute("Behavior")> _
Public Property X() As Integer
Get
Return _X
End Get
Set(ByVal Value As Integer)
_X = Value
End Set
End Property
<DefaultValueAttribute(0), _
CategoryAttribute("Behavior")> _
Public Property Y() As Integer
Get
Return _Y
End Get
Set(ByVal Value As Integer)
_Y = Value
End Set
End Property
<DefaultValueAttribute(0), _
CategoryAttribute("Behavior")> _
Public Property Z() As Integer
Get
Return _Z
End Get
Set(ByVal Value As Integer)
_Z = Value
End Set
End Property
Public Overloads Function Equals(ByVal obj As Object) As Boolean
If obj Is GetType(PointXYZ) Then
Dim comparePoint As PointXYZ = CType(obj, PointXYZ)
If comparePoint._X = Me._X _
AndAlso comparePoint._Y = Me._Y _
AndAlso comparePoint._Z = Me._Z Then
Return True
End If
End If
Return False
End Function
Public Overrides Function GetHashCode() As Integer
Return (_X Xor _Y Xor _Z)
End Function
Public Overloads Function ToString() As String
' Invariant
Return String.Format("{0}, {1}, {2}", _X, _Y, _Z)
End Function
<Browsable(False)> _
Public ReadOnly Property IsEmpty() As Boolean
Get
If _X = 0 AndAlso _Y = 0 AndAlso _Z = 0 Then
Return True
End If
Return False
End Get
End Property
End Structure
#End Region
#Region "PointXYZConverter"
<Serializable()> _
Public Class PointXYZConverter
Inherits ExpandableObjectConverter
Public Overloads Overrides Function CanConvertFrom( _
ByVal context As ITypeDescriptorContext, _
ByVal sourceType As System.Type) As Boolean
If (sourceType Is GetType(String)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
Public Overloads Overrides Function ConvertFrom( _
ByVal context As ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object) As Object
If Not TypeOf value Is String Then
Return MyBase.ConvertFrom(context, culture, value)
End If
Dim stringValue As String = CType(value, String)
If stringValue Is Nothing Or stringValue.Length = 0 Then
Return Nothing
End If
' Trennzeichen entsprechend der Culture
If culture Is Nothing Then
culture = CultureInfo.InvariantCulture
End If
Dim listSeparator As Char() = New Char()
{culture.TextInfo.ListSeparator.Chars(0)}
Dim splitValues As String() = stringValue.Split(listSeparator)
If splitValues.Length = 3 Then
Dim x As Integer = Int32.Parse(splitValues(0), NumberStyles.Integer,
culture)
Dim y As Integer = Int32.Parse(splitValues(1), NumberStyles.Integer,
culture)
Dim z As Integer = Int32.Parse(splitValues(2), NumberStyles.Integer,
culture)
Return New PointXYZ(x, y, z)
End If
Throw New ArgumentException( _
String.Format(culture, "Keine gültige PointXYZ Textangabe '{0}'", value))
End Function
Public Overloads Overrides Function CanConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal destinationType As Type) As Boolean
If destinationType Is GetType(PointXYZ) Then
Return True
End If
If destinationType Is GetType(InstanceDescriptor) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationType)
End Function
Public Overloads Overrides Function ConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object, _
ByVal destinationType As System.Type) As Object
If TypeOf value Is PointXYZ Then
Dim pointValue As PointXYZ = CType(value, PointXYZ)
If destinationType Is GetType(System.String) Then
If culture Is Nothing Then
culture = CultureInfo.InvariantCulture
End If
Dim listSeparator As String = _
culture.TextInfo.ListSeparator & " "
Dim stringValues As String() = New String() _
{pointValue.X.ToString(culture), _
pointValue.Y.ToString(culture), _
pointValue.Z.ToString(culture)}
Return String.Join(listSeparator, stringValues)
ElseIf destinationType Is GetType(InstanceDescriptor) Then
Dim ci As System.Reflection.ConstructorInfo = _
GetType(PointXYZ).GetConstructor( _
New Type() {GetType(Integer), GetType(Integer), GetType(Integer)})
If Not ci Is Nothing Then
Return New InstanceDescriptor(ci, _
New Object() {pointValue.X, pointValue.Y, pointValue.Z})
End If
End If
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
Public Overloads Function GetCreateInstanceSupported( _
ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overloads Function CreateInstance( _
ByVal context As ITypeDescriptorContext, _
ByVal propertyValues As IDictionary) As Object
Return New PointXYZ(DirectCast(propertyValues("X"), Integer), _
DirectCast(propertyValues("Y"), Integer), _
DirectCast(propertyValues("Z"), Integer))
End Function
End Class
#End Region
///

Ähnliche fragen