Help: Using Style Property

R

Ryan

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top