468,140 Members | 1,436 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,140 developers. It's quick & easy.

Designer serialization of non-default values

Imagine I subclass Panel into MyPanel and set a few property values to my
own defaults as shown below. Eg. I set BackColor to by LightYellow.

When I add MyPanel to a form, it will write the non-default property values
into the InitializeComponent of the form. So it will explicitily set
myPanel1.BackColor = Color.LightYellow. If I later change the MyPanel
BackColor default from LightYellow to LightRed, it will not reflect this in
my form since the panel on the form is explicitely set to LightYellow.

I hope that makes sense.

Now, I know I can override BackColor and use the [DefaultValue ... ] option
to define new defaults in MyPanel, but if I'm modifying a dozen properties
then do I really need to this for every modified property?

public class MyPanel : Panel
{
public MyPanel()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.SuspendLayout();
//
// MyPanel
//
this.AutoSize = true;
this.AutoSizeMode =
System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.BackColor = System.Drawing.Color.LightYellow;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.ResumeLayout(false);
}
}

--Charlie
Nov 30 '06 #1
3 4483
Hi Charlie,
Now, I know I can override BackColor and use the [DefaultValue ... ]
option to define new defaults in MyPanel, but if I'm modifying a dozen
properties then do I really need to this for every modified property?
DefaultValueAttribute is the simplest way. Yes, it has to be added to each
property that you want to modify.

In some cases you may have to create a ShouldSerialize[Property] method
instead, but if you're using a default value that can be expressed in a
constant expression then DefaultValueAttribute will work.

"Defining Default Values with the ShouldSerialize and Reset Methods"
http://msdn2.microsoft.com/en-us/library/53b8022e.aspx

--
Dave Sexton

"Charlie" <null@nullwrote in message
news:uM**************@TK2MSFTNGP03.phx.gbl...
Imagine I subclass Panel into MyPanel and set a few property values to my
own defaults as shown below. Eg. I set BackColor to by LightYellow.

When I add MyPanel to a form, it will write the non-default property
values into the InitializeComponent of the form. So it will explicitily
set myPanel1.BackColor = Color.LightYellow. If I later change the MyPanel
BackColor default from LightYellow to LightRed, it will not reflect this
in my form since the panel on the form is explicitely set to LightYellow.

I hope that makes sense.

Now, I know I can override BackColor and use the [DefaultValue ... ]
option to define new defaults in MyPanel, but if I'm modifying a dozen
properties then do I really need to this for every modified property?

public class MyPanel : Panel
{
public MyPanel()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.SuspendLayout();
//
// MyPanel
//
this.AutoSize = true;
this.AutoSizeMode =
System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.BackColor = System.Drawing.Color.LightYellow;
this.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.ResumeLayout(false);
}
}

--Charlie

Nov 30 '06 #2
I was afraid you would say that. Allow me to explain the actual problem I am
having. The control I am trying to subclass is a 3rd party Infragistics
UltraWinGrid control.

In my subclass, I set the this.DisplayLayout.Override.AllowGroupBy property
to false (just an example), and InitializeComponent looks like this:

private void InitializeComponent()
{
((System.ComponentModel.ISupportInitialize)(this)) .BeginInit();
this.SuspendLayout();
//
// MyUltraGrid
//
this.DisplayLayout.Override.AllowGroupBy =
Infragistics.Win.DefaultableBoolean.False;
((System.ComponentModel.ISupportInitialize)(this)) .EndInit();
this.ResumeLayout(false);
}

Looks fine so far. When I add this control to a form, the AllowGroupBy =
False line also appears in the InitializeComponent of the form. This is a
problem because if I later decide to change the default value to True, the
grid on the form will still use the False value!

Furthermore, I cannot override any of these properties
(DefaultValueAttribute, Reset..., ShouldSerialize... do not work) since
AllowGroupBy is a property of the Override property which is a property of
DisplayLayout, etc.

At the end of the day, I have about 50 lines of code which is placed into
InitializeComponent for every grid within my application. Furthermore, if I
change any of these defaults in the subclass, I am forced to manually clean
up the dozens of places where this code has been unnecessarily written.

Is there a solution to this?

--Charlie
Nov 30 '06 #3
Hi Charlie,
>I was afraid you would say that
How did you know that I was going to answer your question? :p
Allow me to explain the actual problem I am having. The control I am
trying to subclass is a 3rd party Infragistics UltraWinGrid control.

In my subclass, I set the this.DisplayLayout.Override.AllowGroupBy
property to false (just an example), and InitializeComponent looks like
this:

private void InitializeComponent()
{
((System.ComponentModel.ISupportInitialize)(this)) .BeginInit();
this.SuspendLayout();
//
// MyUltraGrid
//
this.DisplayLayout.Override.AllowGroupBy =
Infragistics.Win.DefaultableBoolean.False;
((System.ComponentModel.ISupportInitialize)(this)) .EndInit();
this.ResumeLayout(false);
}

Looks fine so far. When I add this control to a form, the AllowGroupBy =
False line also appears in the InitializeComponent of the form. This is a
problem because if I later decide to change the default value to True, the
grid on the form will still use the False value!

Furthermore, I cannot override any of these properties
(DefaultValueAttribute, Reset..., ShouldSerialize... do not work) since
AllowGroupBy is a property of the Override property which is a property of
DisplayLayout, etc.

At the end of the day, I have about 50 lines of code which is placed into
InitializeComponent for every grid within my application. Furthermore, if
I change any of these defaults in the subclass, I am forced to manually
clean up the dozens of places where this code has been unnecessarily
written.

Is there a solution to this?
If you can live with the designer not showing your default values and if you
are sure that you won't even attempt to override the default values using
the designer, then you may be able to use the following code in your derived
Control to set default values only at runtime when the control is becoming
visible:

protected override void OnCreateControl()
{
if (Site == null || !Site.DesignMode)
{
this.DisplayLayout.Override.AllowGroupBy =
Infragistics.Win.DefaultableBoolean.False;
}

base.OnCreateControl();
}

Since you're not dealing with a FCL control I can't test this myself, but I
assume it will work. Let us know :)

Another solution might be creating a custom designer, but you'll have to
derive from Infragistics designer if you want to retain the expected
design-time behavior, which might not be possible and probably wouldn't be
an easy task anyway. If it comes down to that it's probably not worth your
time, but searching or posting in Infragistics forums might help you to find
a better solution.

--
Dave Sexton
Nov 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Daniel Billingsley | last post: by
1 post views Thread by Lucien Dol | last post: by
reply views Thread by Adam Right | last post: by
1 post views Thread by =?Utf-8?B?RjVGNUY1?= | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.