How to avoid a designer creates a Size line for a derived control

M

Mario Vasquez

I am creating a custom button, derived from System.Windows.Forms.Button.
In this new control I assign in the constructor a size for itself,
because I need that to be a fixed one: e.g.,

public CustomButton()
{
....
this.Size = new Size(32, 23);
....
}

since this property is not overridable, I had to create a new property
called Size which hides the base property, so that I can assign a
DefaultValue attribute on it, like so:

[DefaultValue(typeof(Size), DEFAULT_SIZE)]
public new Size Size
{
get { return base.Size; }
set { base.Size = value; }
}

When this button is inserted in a form, the designer always creates a
new line...

this.customButton1.Size = new Size(32, 23);

how to avoid this line (obviously without having to delete it by hand),
so if I change my class' default value, this will be propagated across
my instantiated objects?
 
B

Bob Powell [MVP]

You can create a ShouldSerialize method in your derived control for the
property in question.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
M

Mario Vasquez

I have tried a lot of things, and haven't figured it out yet. I am now
officially in hair-pulling mode :) . As I stated, I am working on a
derived Button.

It seems that ShouldSerializeXXX, needs an implemented property within
the derived class to be called. In other words, you can't use
ShouldSerializeXXXX to determine serialization of an inherited public
property (as far as I have discovered).

First, I did a test with button's Text property. And also, in order to
work, you shouldn't be using DefaultValueAttribute on that property (who
would have thought?)

After a lot of trial and errors, it finally worked, and I could ran some
logic in that method. At last, Text property is defaulted to "", and is
only serialized when different than that.

Now (if only life was such easy), when I try to do the same for the Size
property -my original problem-, well, ShouldSerializeSize is never
called. It happens that Size is not overridable, then I opted to
implement a new wrapper property for the base implementation:

public new Size Size { get; set; }

I also have tried without the new keyword, getting the same outcome.

And still, I can't get ShouldSerializeSize to get called, what can I be
missing?

It seems there's a lot of fine details to implement such behavior, and
so little documentation about it, every little piece of info I have came
across, is the sort-of "use ShouldSerialize method" but making it work,
well, that's another beast.

Thanks for any further help.
 
M

Mario Vasquez

Well, it seems like the Designer is calling directly Control.Size to set
this property, thus making any effort to override the vanilla
functionality, mmm... useless. Because this property is not virtual in
the base class, I am basically creating a new property in the derived
class, but it looks like is never called when working in the designer
environment, except for once, the generated code for instantiation. In
that case, the line generated is something like:

this.customButton1.Size = new System.Drawing.Size(32, 23);

This is the only ocassion where I can count on this custom property
being called. Ironically, this is the line I'd like to supress for
default values.

Consequently, if my custom property (Size) is not being called,
ShouldSerializeSize() will never be called as well.

Now, the usual rant, why the Size property was not defined as virtual?
 
B

Bob Powell [MVP]

You could create a designer for your object and use the PostFilterProperties
method to remove the Size property from the list. You could also make your
object imlement ICustomTypeDescriptor and remove the property in a more
brutal fashion.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
B

Bob Powell [MVP]

A wee bit of code...

[Designer(typeof(UserControlDesignerThing))]

public partial class UserControl1 : UserControl

{

public UserControl1()

{

InitializeComponent();

}

}

class UserControlDesignerThing : ControlDesigner

{

protected override void PostFilterProperties(System.Collections.IDictionary
properties)

{

base.PostFilterProperties(properties);

properties.Remove("Size");

}

}


--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
M

Mario Vasquez

Thanks Bob, that's what I think I needed, a little snippet-push. I am
going to take a deeper look onto this, and get back to you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top