Heh.. . .Font is a different ball of wax. You don't want to use DefaultValue on Font because you can only specify Static values. You should create a ShouldSerializeFont method on the class that defines the property. So, something like this:
<code snippet = C#> private bool ShouldSerializeFont() { return !Font.Equals(new Font("Comic Sans MS", 2, FontStyle.Bold); } </code snippet> Jacob A. Grass > -----Original Message----- > From: Jeff [mailto:[EMAIL PROTECTED]] > Sent: Thursday, May 16, 2002 1:29 PM > To: [EMAIL PROTECTED] > Subject: Re: [DOTNET] DefaultValue() Attribute Question > > > The continuing saga: > > Color worked just fine as noted in earlier replies. > > DefaultValue(typeof(Color), "Control") > > Font is another story. In looking at the info for the DefaultValue > constructor: > > ========================= > [C#] > [AttributeUsage(AttributeTargets.All)] > public DefaultValueAttribute(Type type, string value); > > Parameters > type > A Type that represents the type to convert the value to. > value > A String that can be converted to the type using the > TypeConverter for > the type and the U.S. English culture. > > ========================= > > I can't see why the following would fail for a custom font property: > > DefaultValue(typeof(Font), "Microsoft Sans Serif, 8.25pt") > > Everything compiles okay, but when you look at the unchanged > property in > the property grid, it still highlights bold falsely denoting > a change from > the default value. > > The constructor documentation above says this all uses the > TypeConverter > for the type, so closer experimentation with the > FontConverter yields the > following results where the myControl.AltFont is a certified, > unchanged > default font. > > > new System.Drawing.FontConverter().ConvertToString(myControl.AltFont) > > gives me "Microsoft Sans Serif, 8.25pt". I get the same > result from using > ConvertToInvariantString(). > > Likewise: > > myControl.AltFont = (Font) new System.Drawing.FontConverter > ().ConvertFromString("Microsoft Sans Serif, 8.25pt") > > does yield a good, default font value that I can assign to a > control and > see it really is the plain old vanilla default. > ConvertFromInvariantString > () yields the same results. > > The cast is necessary above because the ConvertFromString > returns an object > rather than a true Font. > > I was concerned that the ColorConverter and the FontConverter were > different here, but the ConvertFromString() methods on both > return object > rather than the type, so the constructor must be handling the > casting down > inside somewhere since it works for Color. > > I don't understand why > > DefaultValue(typeof(Color), "Control") > > works properly to signify a default value for a color in the > property grid, > but > > DefaultValue(typeof(Font), "Microsoft Sans Serif, 8.25pt") > > does not work properly for a font. > > Anyone else know the real story on this one? > > Any help would be appreciated. > > Jeff > > > On Thu, 16 May 2002 06:25:11 -0700, Jeff <[EMAIL PROTECTED]> > wrote: > > >On Thu, 16 May 2002 07:45:51 -0500, Jacob Grass > ><[EMAIL PROTECTED]> wrote: > > > >>DefaultValue(GetType(Color), "Control") > > > >Thanks, I should have read ALL the overloads more carefully > to see the one > >that included type. > > > >However, this still does not work because the compiler reports: > >"'System.Drawing.Color' denotes a 'class' where a 'variable' > was expected" > > > >I did find, however, that the following (C#) DOES work perfectly: > > > >DefaultValue(typeof(Color), "Control") > > > >Thanks so much for pointing me to that overload, DOH!!! > > > >Jeff > > > > You can read messages from the DOTNET archive, unsubscribe > from DOTNET, or > subscribe to other DevelopMentor lists at http://discuss.develop.com. > You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.