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)
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
I haven't looked too closely at your code but I suggest that you open .NET Reflector and take a look at a property that does just that, like the Form.Opacity property. It uses the OpacityConverter class, which you could basically copy.
Hi Jmc, and thanks for the suggestion. Yes this converter should be exactly as the opacity converter. I am not familiar with the .Net Reflector; can you tell me how to open it? Thanks
Last edited by VBDT; Aug 26th, 2007 at 07:13 PM.
Rating is a way of saying thank you. Don't forget to rate always!
You have to download an install .NET Reflector before you can open it, but it's an essential tool for all .NET developers. Just Google it and you find the download. Once you run it you just navigate to the types and members you're interested in and it will show you how they are implemented.
I downloaded it and now I am looking in the Windows.Forms.dll but I am not sure where to look in there. Do you know where it might be by the chance? This is a great tool thank you very much.
Rating is a way of saying thank you. Don't forget to rate always!
Never mind I found it. Thank you very much Jmc! Once again you are a life sever. This thing doesn’t let me give you rep but I promise I will get back to this later time.
Rating is a way of saying thank you. Don't forget to rate always!
I copied the exact code but the result is the same (the property is enabled). But when I use OpacityConverter class it works. Is there some attribute I am missing?
Rating is a way of saying thank you. Don't forget to rate always!
You presumably mustn't have used the exact code because I just did and it worked fine. Here's the code for my test UC and PercentageConverter:
Code:
Imports System.ComponentModel
Imports System.Globalization
Public Class UserControl1
Private _percentage As Double
<TypeConverter(GetType(PercentageConverter)), DefaultValue(1.0R)> _
Public Property Percentage() As Double
Get
Return Me._percentage
End Get
Set(ByVal value As Double)
Me._percentage = value
End Set
End Property
End Class
Public Class PercentageConverter
Inherits TypeConverter
' Methods
Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
Return ((sourceType Is GetType(String)) OrElse MyBase.CanConvertFrom(context, sourceType))
End Function
Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
If Not TypeOf value Is String Then
Return MyBase.ConvertFrom(context, culture, value)
End If
Dim s As String = CStr(value).Replace("%"c, " "c).Trim
Dim num As Double = Double.Parse(s, CultureInfo.CurrentCulture)
If (((CStr(value).IndexOf("%") > 0) AndAlso (num >= 0)) AndAlso (num <= 1)) Then
s = (num / 100).ToString(CultureInfo.CurrentCulture)
End If
Dim num3 As Double = 1
Try
num3 = CDbl(TypeDescriptor.GetConverter(GetType(Double)).ConvertFrom(context, culture, s))
If (num3 > 1) Then
num3 = (num3 / 100)
End If
Catch exception As FormatException
Throw New FormatException("InvalidBoundArgument", exception)
End Try
If ((num3 < 0) OrElse (num3 > 1)) Then
Throw New FormatException("InvalidBoundArgument")
End If
Return num3
End Function
Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
If (destinationType Is Nothing) Then
Throw New ArgumentNullException("destinationType")
End If
If (destinationType Is GetType(String)) Then
Dim num As Double = CDbl(value)
Dim num2 As Integer = CInt((num * 100))
Return (num2.ToString(CultureInfo.CurrentCulture) & "%")
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
End Class
The only change was that I removed the reference to the SR class, which is not available outside the System.Windows.Forms.dll assembly.
Hmm! I tried your code but no result. For SR I just added a reference to the System.Web.Mobile and that is not a problem but still with the two examples the property is unabled. Here is the code I have now.
vb Code:
Imports System.ComponentModel
Imports System.Globalization
Imports System.Web.UI.MobileControls.Adapters
Public Class PercentageConverter
Inherits TypeConverter
' Methods
Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
Return ((sourceType Is GetType(String)) OrElse MyBase.CanConvertFrom(context, sourceType))
End Function
Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
Throw New FormatException(SR.GetString("InvalidBoundArgument", New Object() {"Opacity", s, "0%", "100%"}), exception)
End Try
If ((num3 < 0) OrElse (num3 > 1)) Then
Throw New FormatException(SR.GetString("InvalidBoundArgument", New Object() {"Opacity", s, "0%", "100%"}))
End If
Return num3
End Function
Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
If (destinationType Is Nothing) Then
Throw New ArgumentNullException("destinationType")
I dont think that you need to go to all that trouble of using the reflector as I did custom formatting in an expandable property in 2003 just with a class or two. There is also a GetProperties overrides that should help. It should just be a matter of overriding a few functions and returning the proper valid format of the string?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Yes Rob that is what I am doing, I just wanted to see how the .Net OpacityConverter class is implementing. The problem is that when I debug the UserControl the property is blacked out (unabled) but if I reference the UserControl in some windows application project I can see the property with its value formatted.
I don’t know why it doesn’t work when I debug the UserControl but if I use OpacityConverter class it works perfect.
Rating is a way of saying thank you. Don't forget to rate always!
Well it goes black when there is a bug or incorrect function calls for the property. Sometimes it can even crash your VS IDE when you click into a custom property when there is a bug with it. I know this first hand
If it works with the OPC class then its easier to complete your project but I will try adding a % property to one of my demo UCs I made for a member.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
I used the OpacityConverter class for that property but the odd thing is that it throws “Opacity should be between 0% to 100%” message where my property has nothing to do with opacity if incorect value is entered. Can you take a look on my code and see what is wrong in there. The code I have is the exact copy of OpacityConverter class in .Net but for some reason it doesn’t work correctly. It might be that I should add some attribute to the class but I am not sure. And yes the property is blacked out if I debug the UserControl and if I clicked on it I get an exception “Object reference not set to an instance of an object.”
Rating is a way of saying thank you. Don't forget to rate always!
Ok you must have something going on in your project thats different because I used your code and it works fine. I have it in a project and not the test container.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Yes Rob it works in a project if I reference the UserControl in that project but it doesn’t work in the test container. Also there is a small problem if I use it in some project, that is that if I enter incorrect value it shows an error box but if I click the details button the message is different than what I have in the class.
Rating is a way of saying thank you. Don't forget to rate always!
It could be but if you use OpacityConverter it works perfect! Why this should be different? The code of the class is the same as of OpacityConverter class.
Rating is a way of saying thank you. Don't forget to rate always!
The OpacityConverter probably has all the functions implemented that are required. You may need more overrides as the CanConvertTo function should be used as it is just as much needed as the CanConvertFrom and ConvertFrom functions.
Also available:
GetCreateInstanceSupported, CreateInstance, GetProperties, GetPropertiesSupported
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
It might be true but when I check the OpacityClass in .Net Reflector it shows only these functions (see the image). could it be that the class is in the UserControl library?
Rating is a way of saying thank you. Don't forget to rate always!
Rob there is good news. I just created Class library project and copied the class in there and built it then referenced it in the UserControl project and it fixed it. I guess the problem was that I was using the class in the UserControl project where it should be outside. I don’t now why but it works! Thanks for patience and help also thanks goes to Jmc too. You guys are great !!! And sorry that I can’t give you guys reps for some reason it doesn’t allow me to do (I guess you are special ).
Last edited by VBDT; Aug 27th, 2007 at 01:12 PM.
Rating is a way of saying thank you. Don't forget to rate always!
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
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.
Rating is a way of saying thank you. Don't forget to rate always!
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.
Rating is a way of saying thank you. Don't forget to rate always!
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.
Rating is a way of saying thank you. Don't forget to rate always!
Well other then a message that wont mean much to a user the property is kind of working
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%"}
Rating is a way of saying thank you. Don't forget to rate always!
“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.
Rating is a way of saying thank you. Don't forget to rate always!
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.