1 Attachment(s)
[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:
Public Class PercentageConverter
Inherits TypeConverter
Public Sub New()
End Sub
Public Overloads Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.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 System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
If TypeOf value Is String Then
Dim s As String
s = CStr(value)
If s.EndsWith("%") Then
s = (CStr(value)).Substring(0, (CStr(value)).Length - 1)
End If
Return Double.Parse(s, NumberStyles.Any, culture) / 100
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
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
If destinationType Is GetType(String) Then
Dim v As Double
v = CDbl(value) * 100
Return CStr(v) & "%"
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
End Class
and this is how i use it:
vb Code:
''' <summary>
''' Gets or sets the size of the center-point that is directly proportional to the circumference of the clock.
''' </summary>
<DefaultValue(0.03R), TypeConverterAttribute(GetType(PercentageConverter)), _
Bindable(True), Category("Layout"), Description( _
"Indicates the size of the center-point that is directly proportional to the circumference of the clock.")> _
Public Property CenterPointSize() As Double
'<DebuggerHidden()> _
Get
Return Me._centerPointRatio
End Get
'<DebuggerHidden()> _
Set(ByVal value As Double)
If Not Me._centerPointRatio.Equals(value) Then
If value < 0 Or value > 1 Then
Throw New Exception("The value should be in the range of 0 to 100.")
Else
Me._centerPointRatio = value
Me.SetCenterPointPath()
Me.ClockPanel.Invalidate()
Me.ClockPanel.Update()
End If
End If
End Set
End Property
Re: [RESOLVED] TypeConverter?
I have mine outside the UC base file in a separate file but it seems like placing it in its own dll forces it to create an instance or something that the test container doesnt do.
Re: [RESOLVED] TypeConverter?
Quote:
Originally Posted by RobDog888
I have mine outside the UC base file in a separate file but it seems like placing it in its own dll forces it to create an instance or something that the test container doesnt do.
Yes I guess that is true.
Re: [RESOLVED] TypeConverter?
But I assume you are only going to keep it this way for debugging and testing as it would require an extra file to deploy with your control.
1 Attachment(s)
Re: [RESOLVED] TypeConverter?
Now I have another problem with validation. When I enter let say invalid value of 200 it throws an exception different message. I just left what the OpacityConverter class has in it but the message if different as you can see in the image.
Re: [RESOLVED] TypeConverter?
Quote:
Originally Posted by RobDog888
But I assume you are only going to keep it this way for debugging and testing as it would require an extra file to deploy with your control.
Yes I guess I should have the class in my UserControl project in the control’s final realize. I don’t want to have another dll file for this control. But I need to have this new problem (validation) fixed.
Re: [RESOLVED] TypeConverter?
Well other then a message that wont mean much to a user the property is kind of working :D
When done in the project instead I get this error:
Value cannot be null.
Parameter name: format
Where is "format" argument coming from? I would assume its something behind the scenes with FormatException.
Re: [RESOLVED] TypeConverter?
The correct message should be ...
Value of '200' is not valid for 'Opacity'. 'Opacity' should be between 0% and 100%.
Also, (Edit) you should change the property name in the formatexception to reflect your property and not Opacity.
Code:
Throw New FormatException(SR.GetString("InvalidBoundArgument", New Object() {"CenterPoint", s, "0%", "100%"}))
Re: [RESOLVED] TypeConverter?
Quote:
Originally Posted by RobDog888
Well other then a message that wont mean much to a user the property is kind of working :D
When done in the project instead I get this error:
Value cannot be null.
Parameter name: format
Where is "format" argument coming from? I would assume its something behind the scenes with FormatException.
It might be that it throws another exception on that line I am not sure. And I don’t know where the “format” argument comes from but the problem should be in here:
SR.GetString("InvalidBoundArgument", New Object() {"Opacity", s, "0%", "100%"}
Re: [RESOLVED] TypeConverter?
Yes as I was guessing it is that line. It throws another exception if I do this:
vb Code:
Dim t As String = SR.GetString("InvalidBoundArgument", New Object() {"Opacity", s, "0%", "100%"})
I need to take a look what is happening in here.
Re: [RESOLVED] TypeConverter?
Im assuming its coming from "s" since its supossed to contain the value.
Re: [RESOLVED] TypeConverter?
“s” has a value 200 at the error time so it is not null. I also checked the object array and it contains all the four arguments so this not spouse to be the problem. But I am not sure about the “InvalidBoundArgument” which spouse to be a system string name that my system is missing or maybe I can’t have “System.Web.UI.MobileControls.Adapters” in user control. I am going to pass on this one and just set the message of the exception with out using SR.
Now the control works as it should. One thing that I still will look is to figure out why the properties are blacked out in the test container. I think the reason is that the class doesn’t generate an instance when it is declared as attribute, but there should be some attribute to apply to the class that may fix this problem. Anyways thank you so much Rob for all the help.
Re: [RESOLVED] TypeConverter?
No prob but I dont think I was able to help too much. :(
I still think that when you are doing custom formatting of a property you need to use all four overrides: CanConvertTo, CanConvertFrom, ConvertFrom and ConvertTo.