Wrong size of UserControl

H

Hamed

Hello

I have a UserControl sized to 100; 200
When I drop it in my form the initial size is 100;200 (as I prefer). Then I
decided to change the size of my UserControl to 200;200 but although I have
not set any value to the size of the dropped control in my form, recompiling
the project does not affect the new size of the user control in my form.

Surprisingly if I reset the Size property of my user control in the form,
the size changes to 150; 150! It seems some how, there is a DefaultValue for
Size property.

How can I solve my problem? I prefer when I resize my UserControl in form
designer, it affects all places it is used. another statement is I want my
default size affects from the main UserControl class.

Thanks in advance.
Hamed
 
A

.\\\\axxx

Hello

I have a UserControl sized to 100; 200
When I drop it in my form the initial size is 100;200 (as I prefer). Then I
decided to change the size of my UserControl to 200;200 but although I have
not set any value to the size of the dropped control in my form, recompiling
the project does not affect the new size of the user control in my form.

Surprisingly if I reset the Size property of my user control in the form,
the size changes to 150; 150! It seems some how, there is a DefaultValue for
Size property.

How can I solve my problem? I prefer when I resize my UserControl in form
designer, it affects all places it is used. another statement is I want my
default size affects from the main UserControl class.

Thanks in advance.
Hamed

Unless you add an attribute to your size property in your usercontrol,
it will not have a default size as far as the designer is concerned -
so the designer will add code that sets the size explicitly to the
size set when the control is created at design time.
To prevent this happening you need to tell the designer that this
control's size has a default.YOu should be able to do this using
[DefaultValue(typeof(System.Drawing.Size), "100, 200"] as an attribute
to the size property.

(I actrually just tried this, and it didn't work, as teh IDE seems to
still serialize the Size. As a work around, you can open the
Designer.cs file, and delete the line that sets your contorl's size -
from then on changing your usercontrol
s size will be reflected in the form where it is used. I'm not sure
why the DefaultValue isn't working 'properly' though- but I am sure
someone far cleverer than what I am will let us all know !)
Max
 
H

Hamed

Max,
Thanks for your pay attention but:

1 - how do you assign DefaultValue attribute to the Size property that is
inherited from Control class?

2 - If I delete the line that sets Size, it will be created next time that I
open the form and do some changes.

Any suggestion is welcomed.

Hamed
 
A

Andrej Tozon

Since Size property isn't virtual, overloading isn't possible, however
hiding is. Using the following property declaration in your user
control I believe you will make the designer respect your settings:

[DefaultValue(typeof(Size), "300,300")]
public new Size Size { get; set; }

Just note that property hiding doesn't have the same effect as
overriding and you should be careful with how to use it. With that,
you would still have to "Reset" the Size property on every control of
that type (there's a "cure" for this also, like choosing to not
persist the Size property at all, but I'm not sure that's always a
good idea).
And a suggestion - Once happy with the final layout/size, I would
remove the above property declaration and let the final size persist
as it should.

Andrej
 
B

Bob Powell [MVP]

A quick look with Rflector shosw the following in the UserControl.

protected override Size get_DefaultSize()
{
return new Size(150, 150);
}

Setting the DefaultValue attribute does not change this value. You should
override the property yourself to change it.
You can however set the default value to whatever your derived control
returns so that the designer will not highlight the value with bold text to
indicate that the value is not the default.

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

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.
 

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