locked
Get color from TypeConverter. RRS feed

  • Question

  • Hello

    I working in VB.Net 2010 framework 2.0

    I am using the TypeConverter

    http://msdn.microsoft.com/en-us/library/ayybcxe5.aspx

    How typeconverter can return me the Color instead of string . Currently it is returning the color in form of string. waht i have to do to get the color.

    I an using <TypeConverter(GetType(MyTypeConverter))> for backcolor property of custom panel.

    My requirement is that only for some colors, i need the string and for rest of the colors, i want the original color value of backcolor of panel.

    how can i achieve that?

     

    here is the code i am using

     

    Class CPanel Inherits Panel

     

    <TypeConverter(GetType(MyTypeConverter))> _

    <EditorAttribute(GetType(myUserControlPropertyEditor1), GetType(System.Drawing.Design.UITypeEditor))> _

    Public Overrides Property BackColor() As System.Drawing.Color Get Return MyBase.BackColor End Get Set(ByVal value As System.Drawing.Color) MyBase.BackColor = value End Set End Property

     

    Class MyTypeConverter

        Inherits TypeConverter

     

        ' 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 Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean

            If sourceType Is GetType(String) Then

                Return True

            End If

            'Dim r As Type = Color.Red.GetType

            'MsgBox("TYPE : " & sourceType.ToString & " " & context.PropertyDescriptor.Name & " " & MyBase.CanConvertFrom(context, r))

            Return MyBase.CanConvertFrom(context, sourceType)

        End Function

     

        Private color1 As String = "MyRedColor"

        Private color2 As String = "MyGreenColor"

        Private color3 As String = "MyBlueColor"

     

        ' Overrides the ConvertFrom method of TypeConverter.

        Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As Globalization.CultureInfo, ByVal value As Object) As Object

            Return MyBase.ConvertFrom(context, culture, value)

        End Function

     

        ' Overrides the ConvertTo method of TypeConverter.

        Public Overloads Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object

            If destinationType Is GetType(String) Then

                If TypeOf value Is Color Then

                    If value = Color.Red Then

                        Return color1

                    ElseIf value = Color.Green Then

                        Return color2

                    ElseIf value = Color.Blue Then

                        Return color3

                    End If

                End If

            End If

     

            Return Color.FromName(MyBase.ConvertTo(context, culture, value, destinationType))

            'Return DirectCast(Color.FromArgb(20, 20, 20), Object)

            'Return Color.FromArgb(value)

            'Return (TypeDescriptor.GetConverter(value).ConvertToString(value))

            'Return DirectCast(value, Color)

     

        End Function

     

    End Class

    End Class

     

    Thanks for any reply in advance

     

    Wednesday, December 15, 2010 12:40 PM

Answers

All replies

  • Be aware that Framework 4.0 has a bunch to create parity between VB and C#.

    You probably wont believe it, but that has affected more C# than VB (VB got for instance the automatic properties)

    VB has already build in the type Conversion keywords

     http://msdn.microsoft.com/en-us/library/s2dy91zy.aspx

    However, I'm not aware what you want to convert with the colors

    As the struct is already full of conversion methods.

    http://msdn.microsoft.com/en-us/library/system.drawing.color_methods.aspx


    Success
    Cor
    • Proposed as answer by Kee Poppy Monday, December 20, 2010 11:44 AM
    Wednesday, December 15, 2010 1:40 PM
  • With the help of typeconverter, in UI for a custom panel ---

    For a backcolor property, if user selects the Red color, the string "MyRedColor" will be shown and same for green and blue color.

    If user selects the color other than the above, the same selected color will be shown as it is without any change.

    Wednesday, December 15, 2010 1:46 PM
  • Hi,

    Why not just add three more properties to your custom panel that return the level of RED, GREEN and BLUE components for the BackColor?

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.
    • Proposed as answer by Kee Poppy Monday, December 20, 2010 11:44 AM
    • Marked as answer by Kee Poppy Thursday, December 23, 2010 2:20 AM
    Wednesday, December 15, 2010 2:17 PM
  • Hi Amolpbhavsar,

    Have you solved this issue?

    What John said did make sense. It is better that you create three properties correspond to Red, Green and Blue (as Btye or Integer type). And specify the values independently. Regarding to the custom property grid, the following article may help you: http://www.codeproject.com/KB/dotnet/Property_Editor.aspx

    Best Regards,

     


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 20, 2010 11:44 AM