Textbox mit neuen Eigenschaften austatten

11/11/2007 - 12:18 von Bernd Schend | Report spam
Hallo zusammen,

ich habe eine Klasse CustomTextbox erstellt, die von Textbox erbt.
Dieser Klasse möchte ich nun mit einer Eigenschft namens "Indentations"
ausstatten. "Indentations" ist eine Struktur, die sich aus zwei
Komponenten zusammensetzt (siehe Code im Anhang).
Wie kann ich erreichen, dass die Eigenschaft im Eigenschaftenfenster
einer Textbox "aufklappbar" angezeigt wird?
Also in etwa so:
- Indentations
LeftIndent
RightIndent


Für eure Tipps vielen Dank.
Gruß
Bernd


Definition der neuen Eigenschaft:
==Public Structure IndentationSpec
Dim LeftIndent As Integer
Dim RightIndent As Integer
End Structure

Private MyIndentations As IndentationSpec
<Category("Appearance"), _
Description("Left and right indentation."), _
Browsable(True)> _
Public Property Indentations() As IndentationSpec
Get
Return MyIndentations
End Get
Set(ByVal value As IndentationSpec)
MyIndentations = value
End Set
End Property

Public Function ShouldSerializeIndentations() As Boolean
Return MyIndentations.LeftIndent = 0 And MyIndentations.RightIndent = 0
End Function
 

Lesen sie die antworten

#1 Karsten Sosna
11/11/2007 - 12:55 | Warnen spam
ich habe eine Klasse CustomTextbox erstellt, die von Textbox erbt.
Dieser Klasse möchte ich nun mit einer Eigenschft namens "Indentations"
ausstatten. "Indentations" ist eine Struktur, die sich aus zwei
Komponenten zusammensetzt (siehe Code im Anhang).
Wie kann ich erreichen, dass die Eigenschaft im Eigenschaftenfenster
einer Textbox "aufklappbar" angezeigt wird?
Also in etwa so:
- Indentations
LeftIndent
RightIndent

Definition der neuen Eigenschaft:
==> Public Structure IndentationSpec
Dim LeftIndent As Integer
Dim RightIndent As Integer
End Structure

Private MyIndentations As IndentationSpec
<Category("Appearance"), _
Description("Left and right indentation."), _
Browsable(True)> _
Public Property Indentations() As IndentationSpec
Get
Return MyIndentations
End Get
Set(ByVal value As IndentationSpec)
MyIndentations = value
End Set
End Property

Public Function ShouldSerializeIndentations() As Boolean
Return MyIndentations.LeftIndent = 0 And MyIndentations.RightIndent = 0
End Function



Hallo Bernd,
da fehlt ja noch ein bißchen was. ;=)
Hier ein Beispiel:
\\\
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
///
Gruß Scotty

Ähnliche fragen