Help: Using Style Property

Discussion in 'ASP .Net Web Controls' started by Ryan, Feb 28, 2007.

  1. Ryan

    Ryan Guest

    HELP!!!

    I have created a custom contol with the following property:

    <TypeConverter(GetType(TypeConverters.StyleConverter)),
    Bindable(True), Category("Header"), Description("HeaderStyle")> _
    Public Property HeaderStyle() As Style
    Get
    EnsureChildControls()
    Return lblHeader.ControlStyle
    End Get
    Set(ByVal value As Style)
    Stop
    EnsureChildControls()
    lblHeader.ControlStyle.CopyFrom(value)
    End Set
    End Property

    The TYPECONVERTER I have added because, without it, when I switch from
    design view to source view and back I will get an error about
    converting from a string. The problem is I really don't understand
    how to use the type converter. I have writen the code below, and not
    when I view my control in the designer the nice drop down list of
    style attributes is gone. What am I missing?

    My TypeConverters.StyleConverter code is below (I have been playing
    with it trying to get it to work so please forgive the commented out
    code and any STOPS:
    --------------------------------
    Imports System
    Imports System.ComponentModel
    Imports System.Globalization
    Imports System.Drawing
    Imports System.Web.UI.WebControls



    Namespace TypeConverters

    Public Class StyleConverter
    Inherits ExpandableObjectConverter

    ' Overrides the CanConvertFrom method of TypeConverter.
    ' The ITypeDescriptorContext interface provides the context
    for the
    ' conversion. Typically, this interface is used at design time
    to
    ' provide information about the design-time container.

    Public Overrides Function CanConvertTo(ByVal context As
    System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As
    System.Type) As Boolean
    If destinationType Is GetType(String) Then
    Return True
    End If
    Return MyBase.CanConvertTo(context, destinationType)
    End Function
    Public Overloads Overrides Function CanConvertFrom(ByVal
    context As ITypeDescriptorContext, ByVal sourceType As Type) As
    Boolean
    If sourceType Is GetType(Style) Then
    Return True
    End If
    Return MyBase.CanConvertFrom(context, sourceType)
    End Function

    Public Overrides Function GetCreateInstanceSupported(ByVal
    context As ITypeDescriptorContext) As Boolean
    ' Always force a new instance
    Return True
    End Function


    ' Overrides the CreateInstance method of
    ExpandableTypeConverter
    Public Overrides Function CreateInstance(ByVal context As
    System.ComponentModel.ITypeDescriptorContext, ByVal propertyValues As
    System.Collections.IDictionary) As Object
    Stop
    Return New Style
    End Function

    Public Overloads Function ConvertTo(ByVal context As
    ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As
    Object) As Object
    'Do I need this? Should it be Overloads?

    End Function

    ' Overrides the ConvertFrom method of ExpandableTypeConverter.
    Public Overloads Overrides Function ConvertFrom(ByVal context
    As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value
    As Object) As Object
    If TypeOf value Is String Then
    ' Dim item As Object
    Dim thisStyle As New Style
    'thisStyle.BackColor
    'thisStyle.BorderColor
    'thisStyle.BorderStyle
    'thisStyle.CssClass
    'thisStyle.Font.Names
    'thisStyle.Font.Name
    'thisStyle.Font.Overline
    'thisStyle.ForeColor
    'thisStyle.Height
    'thisStyle.Font.Size
    ' For Each item In value
    ' thisstyle.
    ' Next
    ' Dim v As String() =
    CStr(value).Split(New Char() {";"})
    ' For Each item In v
    ' thisStyle.Container.Add(item)
    ' Next
    Return thisStyle
    End If
    Return MyBase.ConvertFrom(context, culture, value)
    End Function

    ' Overrides the ConvertTo method of ExpandableTypeConverter.
    Public Overloads Overrides Function ConvertTo(ByVal context As
    ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As
    Object, ByVal destinationType As Type) As Object
    If destinationType Is GetType(String) Then
    Dim str As String
    Dim item As Object
    str = ""
    For Each item In value.container
    str = str & item.ToString & ";"
    Next
    Return str
    End If
    Return MyBase.ConvertTo(context, culture, value,
    destinationType)
    End Function
    End Class


    End Namespace
     
    Ryan, Feb 28, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    25
    Views:
    1,495
    Nathan Sokalski
    Mar 4, 2010
  2. Phlip
    Replies:
    2
    Views:
    307
    Phlip
    Oct 23, 2010
  3. Terry Reedy
    Replies:
    5
    Views:
    530
    Robert Kern
    Nov 30, 2010
  4. Ken Varn
    Replies:
    0
    Views:
    525
    Ken Varn
    Apr 26, 2004
  5. Sky
    Replies:
    2
    Views:
    304
    Robert Koritnik
    Oct 1, 2004
Loading...

Share This Page