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 / Windows Forms / Design Time / July 2008

Tip: Looking for answers? Try searching our database.

Changing the designer DefaultValue for a Property within a Property (Nested Property)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gregg Walker - 08 May 2008 00:38 GMT
Hi,

I have an inherited control that derives from a control from a vendor.
Let's call the vendor control VC and my inherited control myVC.

VC has a property Calendar which itself has various other properties such as
BackColor.  So to access in code from myVC i would use
this.Calendar.BackColor syntax.

The vendor has changed the default value for Calendar.BackColor property and
I want to be able to override the default value with the prior default in
myVC.

I'm not sure how to do this.  I was looking at the ShouldSerializeXXXX and
ResetXXXX methods but I don't know how to make them work with nested
properties.

I tried this could but it did not work.

 private bool ShouldSerializeCalendar_BackColor()
 {
  return Calendar.BackColor != SystemColors.Window;
 }

 private void ResetCalendar_BackColor()
 {
  Calendar.BackColor = SystemColors.Window;
 }

Is this possible and if so how would one go about implementing in the
derived class?
--
Gregg Walker
Linda Liu[MSFT] - 08 May 2008 12:16 GMT
Hi Gregg,

Firstly, to set a default value for a property of a class, we set the value
of this property in the class's constructor first and then use the
DefaultValueAttribute on the property to specify the default value. In the
Properties window, any values other than the default value of the property
will shown in a bold font.

Secondly, if a property is overriable, we can derive from the class and
override the property. Change the initial value of the property in the
derived class's constructor and adorn the DefaultAttribute on the override
property to specify the new default value so as to change the default value
of the property.

To your question, if the Calendar property is overriable in the VC class,
you can add a new property in the MyVC class to hide the Calendar property.
The following is a sample to do this.

public partial class VC : UserControl
   {
       public VC()
       {
           InitializeComponent();
           calendar = new MonthCalendar();          
       }
       private MonthCalendar calendar;

       [TypeConverter(typeof(ExpandableObjectConverter)),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
       public virtual MonthCalendar Calendar
       {
           get { return calendar; }
           set { calendar = value; }
       }        
   }

public class DerivedType:MonthCalendar
   {
       public DerivedType()
       {
           BackColor = Color.Red;
       }
       [DefaultValue(typeof(Color),"Red")]
       public override Color BackColor
       {
           get
           {
               return base.BackColor;
           }
           set
           {
               base.BackColor = value;
           }
       }
   }

public partial class MyVC : VC
{
       public MyVC()
       {
           InitializeComponent();
           calendar = new DerivedType();
       }
       private DerivedType calendar;

       
[TypeConverter(typeof(ExpandableObjectConverter)),DesignerSerializationVisib
ility(DesignerSerializationVisibility.Content)]
       public new DerivedType Calendar
       {
           get { return calendar; }
           set { calendar = value; }
       }
}

At last, the ShouldSerialize*** method is used by designer to determine
whether the specified property should be serialized in the
InitializeComponent method. The Reset*** method is used by the designer to
reset the property to the default value in the Properties window. The
DefaultValueAttribute does the all the work these two methods do.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Gregg Walker - 08 May 2008 18:30 GMT
Hi Linda,

Thanks for taking the time to provide the sample code.  Unfortunately the
Calendar property is not virtual and therefore not overrideable.

The closest I have been able to get is with a surrogate property in MyVC
derived control.  Here's the code...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyCalendar
{
public partial class MyVC : C1.Win.C1Input.C1DateEdit
{
 public MyVC()
 {
  InitializeComponent();

  Calendar.BackColor = SystemColors.Window;
  Calendar.ForeColor = SystemColors.WindowText;
 }

 // Surrogate Property for Calendar.BackColor
 [Browsable(true)]
 [Description("Calendar Background Color")]
 [RefreshProperties(RefreshProperties.All)]
 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
 public Color CalendarBackColor
 {
  get
  {
   return Calendar.BackColor;
  }

  set
  {
   Calendar.BackColor = value;
  }
 }

 private bool ShouldSerializeCalendarBackColor()
 {
  return Calendar.BackColor != SystemColors.Window;
 }

 private void ResetCalendarBackColor()
 {
  Calendar.BackColor = SystemColors.Window;
 }

 // Surrogate Property for Calendar.ForeColor
 [Browsable(true)]
 [Description("Calendar Foreground Color")]
 [RefreshProperties(RefreshProperties.All)]
 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
 public Color CalendarForeColor
 {
  get
  {
   return Calendar.ForeColor;
  }

  set
  {
   Calendar.ForeColor = value;
  }
 }

 private bool ShouldSerializeCalendarForeColor()
 {
  return Calendar.ForeColor != SystemColors.WindowText;
 }

 private void ResetCalendarForeColor()
 {
  Calendar.ForeColor = SystemColors.WindowText;
 }
}
}

This works somewhat but now I've got two places I can set the Calendar
BackColor (1=Calendar.BackColor 2=CalendarBackColor) and each with its own
default.  Also even when the CalendarBackColor is set to the default value
code is still generated in the designer class to set the default values in
the Calendar.BackColor property.

There's got to be an easier way to set the default value for a nested
property.  Do you have any other suggestions?
--
Gregg Walker
Linda Liu[MSFT] - 09 May 2008 06:58 GMT
Hi Gregg,

Thank you for your reply!

You have mentioned "The vendor has changed the default value for
Calendar.BackColor property" in your first message. How did the vendor
change the default value for the Calendar.BackColor property within the VC
class?

If the Calendar property in the VC class is not overridable, I don't think
we can change the default value of the nested properties of the Calendar
property from within the MyVC class.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Gregg Walker - 09 May 2008 17:19 GMT
Hi Linda,

Thanks for your help with this issue.

> You have mentioned "The vendor has changed the default value for
> Calendar.BackColor property" in your first message. How did the vendor
> change the default value for the Calendar.BackColor property within the VC
> class?

The Calendar property is a DateEditMonthCalendar class which is part of the
vendor components package.  They changed the default value of the BackColor
property to SystemColors.Control from SystemColors.Window in the
DateEditMonthCalendar class in their latest revision.

> If the Calendar property in the VC class is not overridable, I don't think
> we can change the default value of the nested properties of the Calendar
> property from within the MyVC class.

That's too bad there's not a way to override the default values for nested
properties in a derived class without overriding the parent property itself.
It would be nice if ShouldSerialize and Reset could be modified to work with
nested properties.  I would appreciate it if you could pass this on as a
request for future consideration.

Thanks again for your help.
--
Gregg Walker
Linda Liu[MSFT] - 14 May 2008 06:56 GMT
Hi Gregg,

Thank you for your reply!

I understand your concern. Although there's no way to override the default
value of the BackColor property of the Calender property in the MyVC class
if the Calendar property defined in the VC class is not overridable, we can
change the initial value of the Calender.BackColor property in the MyVC
class's constructor.

Ok, no problem. I will pass your feedback to our product team for future
consideration.

If you have any other questions in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
eschneider - 23 Jul 2008 22:13 GMT
You should be able to shadow the property "new" in C# or "shadows" in VB.

Then change the attributes as you like.

Schneider

> Hi Gregg,
>
[quoted text clipped - 25 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.

Rate this thread:







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.