Results 1 to 37 of 37

Thread: [RESOLVED] TypeConverter?

Threaded View

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Resolved [RESOLVED] TypeConverter?

    Hi everyone, I have a UserControl that I want to show some of its properties whit the percentage char like (100%) so I created a class that inherits from TypeConverter class. For some reason it doesn’t work see the image. What could be the problem?
    Thanks in advance!

    Here is the class:
    vb Code:
    1. Public Class PercentageConverter
    2.         Inherits TypeConverter
    3.  
    4.         Public Sub New()
    5.  
    6.         End Sub
    7.  
    8.         Public Overloads Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
    9.             If sourceType Is GetType(String) Then
    10.                 Return True
    11.             End If
    12.             Return MyBase.CanConvertFrom(context, sourceType)
    13.         End Function
    14.  
    15.         Public Overloads Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
    16.             If TypeOf value Is String Then
    17.                 Dim s As String
    18.                 s = CStr(value)
    19.                 If s.EndsWith("%") Then
    20.                     s = (CStr(value)).Substring(0, (CStr(value)).Length - 1)
    21.                 End If
    22.                 Return Double.Parse(s, NumberStyles.Any, culture) / 100
    23.             End If
    24.             Return MyBase.ConvertFrom(context, culture, value)
    25.         End Function
    26.  
    27.         Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
    28.             If destinationType Is GetType(String) Then
    29.                 Dim v As Double
    30.                 v = CDbl(value) * 100
    31.                 Return CStr(v) & "%"
    32.             End If
    33.             Return MyBase.ConvertTo(context, culture, value, destinationType)
    34.         End Function
    35.  
    36.     End Class
    and this is how i use it:
    vb Code:
    1. ''' <summary>
    2.     ''' Gets or sets the size of the center-point that is directly proportional to the circumference of the clock.
    3.     ''' </summary>
    4.     <DefaultValue(0.03R), TypeConverterAttribute(GetType(PercentageConverter)), _
    5.     Bindable(True), Category("Layout"), Description( _
    6.     "Indicates the size of the center-point that is directly proportional to the circumference of the clock.")> _
    7.       Public Property CenterPointSize() As Double
    8.         '<DebuggerHidden()> _
    9.         Get
    10.             Return Me._centerPointRatio
    11.         End Get
    12.         '<DebuggerHidden()> _
    13.         Set(ByVal value As Double)
    14.             If Not Me._centerPointRatio.Equals(value) Then
    15.                 If value < 0 Or value > 1 Then
    16.                     Throw New Exception("The value should be in the range of 0 to 100.")
    17.                 Else
    18.                     Me._centerPointRatio = value
    19.                     Me.SetCenterPointPath()
    20.                     Me.ClockPanel.Invalidate()
    21.                     Me.ClockPanel.Update()
    22.                 End If
    23.             End If
    24.         End Set
    25.     End Property
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width