473,226 Members | 1,701 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,226 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 4715
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.