473,396 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4729
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Daniel Billingsley | last post by:
I've got a project I've been working on for a few weeks. I've been using the BindingSource control - I've got four of them on this particular form. This morning I went to view the report in the...
1
by: Lucien Dol | last post by:
Hi all, Can someone help me with this, please? I've got a user control that (amongst other controls) contains a textbox. The textbox has its Anchor property set to "Left, Top, Right", meaning...
2
by: Richard Bysouth | last post by:
Hi When attempting to view inherited forms in design mode I have been getting the message "The path is not of a legal form" and am unable to view the designer. I can't seem to find any...
0
by: Adam Right | last post by:
Hi, I am using contextmenustrip in my usercontrol in which there is a grid control associated with it. I added the rows below in the designer code, to create contextmenustrip with its...
0
by: Bruce HS | last post by:
In VS2005, VB 2005, I suddenly can't get at a winform design view. The application still runs OK, but I can't see the designer. I'ver rebuilt the solution with no success. I'm looking for a...
1
by: dusanv | last post by:
Hi, I have a class (say O1) that overrides System::Windows::Forms::UserControl and then another one that overrides O1 (call it O2). O2 used to work with the Designer as it was inheriting...
5
by: Alan T | last post by:
I got this kind of error recently: When I tried to open a form I got error something like this: One of more errors encountered while loading the designer. The errors are listed below. Some...
4
by: ThunderMusic | last post by:
Hi, I have a custom form that works fine when I debug it or run it in release mode but cannot be loaded in the designer... Actually, it can be loaded in the designer when no control is on it, but...
1
by: =?Utf-8?B?RjVGNUY1?= | last post by:
I have created a control by inheriting ToolStripMenuItem that has an image property called LargeImage. ------ public class EventSubMenuItem : ToolStripMenuItem { private Image _largeImage =...
3
by: steve | last post by:
VS designer has started to complain about the following errors : Object reference not set to an instance of an object. Instances of this error (2) and Index was out of range. Must be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.