Web Custom Control - (sub)Properties

Clif001

Newcomer
Joined
Aug 23, 2004
Messages
1
I am writing a Web Custom Control encapsulating a calendar dll I wrote.

I wanted sub-properties in the properties page for the Heading and DayNames (similar to the properties for "Font"). I could not find VB code to do this, so I used an on-line converter to convert C# code. It almost works.

There are two problems.
1. While in development mode, changing any of the sub-properties does not immediately show. However, if I then change one of the non sub-properties, the changes do show up.

2. The properties don't appear to be persistant. That is, if I drop the control on a web page and set the properties, none of the sub-property settings appear when the page is viewed within a browser. The normal properties do appear correctly.

Setting all properties in code works fine.

Code:
'Declare an instance of the dll
    Private m_calMain As New BCP.Web.Calendar.BaseCalendar

'Create module level properties so values can be passed directly to and from m_calMain
    Private m_cpHeading As CellProperties = New CellProperties(m_calMain.Heading)
    Private m_cpDayNames As CellProperties = New CellProperties(m_calMain.DayNames)

'Expose Properties:
    <Bindable(False), Category("Layout"), DefaultValue(166)> Public Overrides Property [Height]() As WebControls.Unit
        Get
            'Pass back value directly from m_calMain
            Return WebControls.Unit.Pixel(m_calMain.Height)
        End Get
        Set(ByVal Value As WebControls.Unit)
            'Pass value directly to m_calMain
            m_calMain.Height = CType(Value.Value, Integer)
        End Set
    End Property
    <Bindable(False), Category("Layout"), DefaultValue(166)> Public Overrides Property [Width]() As WebControls.Unit
        Get
            'Pass back value directly from m_calMain
            Return WebControls.Unit.Pixel(m_calMain.Width)
        End Get
        Set(ByVal Value As WebControls.Unit)
            'Pass value directly to m_calMain
            m_calMain.Width = CType(Value.Value, Integer)
        End Set
    End Property
    <Bindable(False), Category("Appearance"), DefaultValue(""), TypeConverter(GetType(CellProperties))> Public Property [Heading]() As CellProperties
        Get
            Return m_cpHeading
        End Get
        Set(ByVal Value As CellProperties)
            m_cpHeading = Value
        End Set
    End Property
    <Bindable(False), Category("Appearance"), DefaultValue(""), TypeConverter(GetType(CellProperties))> Public Property [DayNames]() As CellProperties
        Get
            Return m_cpDayNames
        End Get
        Set(ByVal Value As CellProperties)
            m_cpDayNames = Value
        End Set
    End Property


'Setup TypeConverter for Cell Properties
<TypeConverter(GetType(CellPropertiesConverter))> _
Public Class CellProperties
    'Declare m_cpProps so we can pass the values directly to m_calMain
    Private m_cpProps As BCP.Web.Calendar.CellProperties
    Public Sub New()
        'This is needed for Function ConvertFrom()
        m_cpProps = New BCP.Web.Calendar.CellProperties
    End Sub
    Public Sub New(ByVal cpProps As BCP.Web.Calendar.CellProperties)
        m_cpProps = cpProps
    End Sub
    Public Property [FontName]() As String
        Get
            'Pass back value directly from m_cpProps which is the same as m_calMain
            Return m_cpProps.FontName
        End Get
        Set(ByVal Value As String)
            'Pass value directly to m_cpProps which is the same as m_calMain
            m_cpProps.FontName = Value
        End Set
    End Property
    Public Property [FontSize]() As Integer
        Get
            'Pass back value directly from m_cpProps which is the same as m_calMain
            Return m_cpProps.FontSize
        End Get
        Set(ByVal Value As Integer)
            'Pass value directly to m_cpProps which is the same as m_calMain
            m_cpProps.FontSize = Value
        End Set
    End Property
    Public Property [FontWeight]() As String
        Get
            'Pass back value directly from m_cpProps which is the same as m_calMain
            Return m_cpProps.FontWeight
        End Get
        Set(ByVal Value As String)
            'Pass value directly to m_cpProps which is the same as m_calMain
            m_cpProps.FontWeight = Value
        End Set
    End Property
    Public Property TextAlignH() As String
        Get
            'Pass back value directly from m_cpProps which is the same as m_calMain
            Return m_cpProps.TextAlignH
        End Get
        Set(ByVal Value As String)
            'Pass value directly to m_cpProps which is the same as m_calMain
            m_cpProps.TextAlignH = Value
        End Set
    End Property
    Public Property TextAlignV() As String
        Get
            'Pass back value directly from m_cpProps which is the same as m_calMain
            Return m_cpProps.TextAlignV
        End Get
        Set(ByVal Value As String)
            'Pass value directly to m_cpProps which is the same as m_calMain
            m_cpProps.TextAlignV = Value
        End Set
    End Property
    Public Property [ForeColor]() As Color
        Get
            'Pass back value directly from m_cpProps which is the same as m_calMain
            Return m_cpProps.ForeColor
        End Get
        Set(ByVal Value As Color)
            'Pass value directly to m_cpProps which is the same as m_calMain
            m_cpProps.ForeColor = Value
        End Set
    End Property
    Public Property [BackColor]() As Color
        Get
            'Pass back value directly from m_cpProps which is the same as m_calMain
            Return m_cpProps.BackColor
        End Get
        Set(ByVal Value As Color)
            'Pass value directly to m_cpProps which is the same as m_calMain
            m_cpProps.BackColor = Value
        End Set
    End Property
End Class


Friend Class CellPropertiesConverter
    Inherits ExpandableObjectConverter

    Friend Overloads Function CanConvertFrom(ByVal context As ITypeDescriptorContext, _
                                             ByVal t As Type) As Boolean
        If TypeOf (t) Is String Then
            Return True
        End If
        Return MyBase.CanConvertFrom(context, t)
    End Function

    Friend Overloads Function ConvertFrom(ByVal context As ITypeDescriptorContext, _
                                          ByVal info As System.Globalization.CultureInfo, _
                                          ByVal value As Object) As Object
        'Dim arrTemp As Array
        Dim arrTemp() As String
        Try
            If TypeOf value Is String Then
                Dim s As String = CType(value, String)
                ' parse the format "FontName, FontSize, FontWeight, TextAlignH, TextAlignV, ForeColor, BackColor"
                '
                arrTemp = Split(s, ",")
                Dim cp As CellProperties = New CellProperties
                cp.FontName = arrTemp(0)
                cp.FontSize = CType(arrTemp(1), Integer)
                cp.FontWeight = arrTemp(2)
                cp.TextAlignH = arrTemp(3)
                cp.TextAlignV = arrTemp(4)
                cp.ForeColor = HexToColor(arrTemp(5))
                cp.BackColor = HexToColor(arrTemp(6))
                Return cp
            End If
        Catch
            ' if we got this far, complain that we
            ' couldn't parse the string
            '
            Throw New ArgumentException( _
               "Can not convert '" & CType(value, String) & _
                        "' to type CellProperties")

        End Try
        Return MyBase.ConvertFrom(context, info, value)
    End Function

    Friend Overloads Function ConvertTo(ByVal context As ITypeDescriptorContext, _
                                        ByVal culture As System.Globalization.CultureInfo, _
                                        ByVal value As Object, _
                                        ByVal destType As Type) As Object
        If TypeOf destType Is String And TypeOf value Is CellProperties Then
            Dim cp As CellProperties = CType(value, CellProperties)
            ' simply build the string as "FontName, FontSize, FontWeight, TextAlignH, TextAlignV, ForeColor, BackColor"
            Return cp.FontName & ", " & _
                cp.FontSize.ToString() & ", " + _
                cp.FontWeight + ", " & _
                cp.TextAlignH + ", " & _
                cp.TextAlignV + ", " & _
                ColorToHex(cp.ForeColor) & ", " & _
                ColorToHex(cp.BackColor) & ""
        End If
        Return MyBase.ConvertTo(context, culture, value, destType)
    End Function
    Private Function ColorToHex(ByVal colValue As Color) As String
        Return "#" & Hex(colValue.ToArgb).Substring(2)
    End Function
    Private Function HexToColor(ByVal sValue As String) As Color
        Dim sColorHex As String
        If sValue.StartsWith("#") Then
            sColorHex = sValue.Substring(1)
        Else
            sColorHex = sValue
        End If
        Return Color.FromArgb(Convert.ToInt32("FF" & sColorHex, 16))
    End Function
End Class
 
Back
Top