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 / WinForm General / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mario Vasquez - 08 Nov 2006 22:06 GMT
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?
Bob Powell [MVP] - 09 Nov 2006 06:58 GMT
You can create a ShouldSerialize method in your derived control  for the
property in question.

Signature

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.

>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
[quoted text clipped - 26 lines]
> if I change my class' default value, this will be propagated across my
> instantiated objects?
Mario Vasquez - 09 Nov 2006 17:39 GMT
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.

> You can create a ShouldSerialize method in your derived control  for the
> property in question.
Mario Vasquez - 09 Nov 2006 19:04 GMT
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?
Bob Powell [MVP] - 10 Nov 2006 10:51 GMT
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.

Signature

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.

> Well, it seems like the Designer is calling directly Control.Size to set
> this property, thus making any effort to override the vanilla
[quoted text clipped - 14 lines]
>
> Now, the usual rant, why the Size property was not defined as virtual?
Bob Powell [MVP] - 10 Nov 2006 10:58 GMT
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");

}

}

Signature

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.

> Well, it seems like the Designer is calling directly Control.Size to set
> this property, thus making any effort to override the vanilla
[quoted text clipped - 14 lines]
>
> Now, the usual rant, why the Size property was not defined as virtual?
Mario Vasquez - 13 Nov 2006 15:52 GMT
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.

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.