Results 1 to 4 of 4

Thread: [RESOLVED] Custom "Override" Structure

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Resolved [RESOLVED] Custom "Override" Structure

    Okay, essentially I have a drawing application, where I have custom classes for the various shape types (Circle, Rectangle, Star, etc.). I'm using the PropertyGrid control within the application to allow the user to change the properties of these shape classes (for example: location, dimensions, etc.). The problem I have is that the ToString method of the native PointF structure (System.Drawing.PointF) gives the following string:
    Code:
    {X=###, Y=###}
    This output is not desirable. I want it to be displayed how it is in the Designer Properties Panel. For example, the "Location" property of a control is displayed as:
    Code:
    X, Y
    1. Is there a way to override the ToString method of the System.Drawing.PointF structure?

    2. Is there a different way to display the properties of the System.Drawing.PointF structure in the PropertyGrid within the application?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Custom "Override" Structure

    You can't override anything in a structure because you can't inherit a structure.

    The behaviour you want is achieved using a TypeConverter. Check out the documentation for the Point structure and the PointF structure and compare the declarations of each. Notice that the Point structure is decorated with the TypConverter attribute and the PointF structure is not.

    I'm not sure but it may be that, if you decorate your property with the TypeConverter attribute, you will get the behaviour you want. Of course, if the Framework contains no suitable TypeConverter class for the PointF structure, you'll have to define your own.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Huntsville, AL
    Posts
    62

    Re: Custom "Override" Structure

    That's exactly what I was looking for!

    I had to create my own TypeConverter. I'm not sure if I'm doing it the best way...but it works. The following is the snippet where the conversion happens:

    Code:
        Public 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.Equals(GetType(String)) Then
                Dim p As PointF = CType(value, PointF)
    
                Return String.Format("{0}, {1}", p.X, p.Y)
            Else
                Return MyBase.ConvertTo(context, culture, value, destinationType)
            End If
        End Function

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: [RESOLVED] Custom "Override" Structure

    I would suggest that you use .NET Reflector to see how it's done in the PointConverter class and then do it the same way.

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