Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2004

Tip: Looking for answers? Try searching our database.

The Size Type/Property

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andre - 20 Sep 2004 11:40 GMT
Please, post the complete csharp code to implement a property like the Size
one.
Including the "Width" and "Height" order of appearance in the designer.
Thanks.
Jakob Christensen - 20 Sep 2004 14:21 GMT
To do that you need to implement a TypeConverter for your type.  I wrote the
following code to show how to do this for at type named Test which has two
properties X and Y (similar to Width and Height of Size):

<code>
   [TypeConverter(typeof(TestTypeConverter))]
   public struct Test
   {
       int x;
       int y;

       public Test(int x, int y)
       {
           this.x = x;
           this.y = y;
       }

       public int X
       {
           get { return x; }
           set { x = value; }
       }

       public int Y
       {
           get { return y; }
           set { y = value; }
       }
   }

   public class TestTypeConverter : TypeConverter
   {
       public TestTypeConverter()
       {
       }

       public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
       {
           if (sourceType == typeof(string))
               return true;

           return base.CanConvertFrom (context, sourceType);
       }

       public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
       {
           if (destinationType == typeof(InstanceDescriptor))
               return true;

           return base.CanConvertTo (context, destinationType);
       }

       public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
       {
           if (value is string)
           {
               string[] v = ((string) value).Split(new char[] {','});
               return new Test(int.Parse(v[0]), int.Parse(v[1]));
           }
           return base.ConvertFrom(context, culture, value);
       }

       public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
       {  
           if (value is Test)
           {
               if (destinationType == typeof(string))
               {
                   return ((Test) value).X + "," + ((Test) value).Y;
               }
               else if (destinationType == typeof(InstanceDescriptor))
               {
                   Test t = (Test) value;

                   ConstructorInfo ctor = typeof(Test).GetConstructor(new
Type[] {typeof(int), typeof(int)});
                   if (ctor != null)
                   {
                       return new InstanceDescriptor(ctor, new object[]
{t.X, t.Y});
                   }
               }
           }
           return base.ConvertTo(context, culture, value, destinationType);
       }

       public override bool GetPropertiesSupported(ITypeDescriptorContext
context)
       {
           return true;
       }

       public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
       {
           return TypeDescriptor.GetProperties(typeof(Test));
       }

    }

</code>

HTH, Jakob.

> Please, post the complete csharp code to implement a property like the Size
> one.
> Including the "Width" and "Height" order of appearance in the designer.
> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.