473,508 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrong size of UserControl

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
Jun 27 '08 #1
4 9287
On May 28, 1:09 am, "Hamed" <ha...@raymehr.netwrote:
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
Jun 27 '08 #2
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

>
(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

Jun 27 '08 #3
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

On 28 maj, 08:04, "Hamed" <ha...@raymehr.netwrote:
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 thatI
open the form and do some changes.

Any suggestion is welcomed.

Hamed


(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- Skrij navedeno besedilo -

- Prikaži navedeno besedilo -
Jun 27 '08 #4
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.

"Hamed" <ha***@raymehr.netwrote in message
news:OD**************@TK2MSFTNGP03.phx.gbl...
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
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7471
by: Lecture Snoddddgrass | last post by:
Hi, When I create a UserControl-derived object, I often expose various public properties. One of the things that I hate about the WinForms designer is that if I decide to embed one of these...
4
1864
by: Derrick | last post by:
Hello all; I'm trying to simulate a "scrollable context menu". What I want is to have a listbox fill the entire area of a small form (with no border). Using the designer and property grid, I...
0
5030
by: Miquel | last post by:
Hi all. I felt frustrated when developing an 'UserControl' derivated from textBox, because sequence event (and Validate event) seems to fail. I Always thought my code was wrong. But after...
4
1565
by: JMUApache | last post by:
Hi, A have a problem with Inhreit Control. I try to Change its Size in Construct Function, And it works. Code Here: -------------------------------------------------- public class...
2
4592
by: Sascha | last post by:
Hi there, I searched carefully through the web before finally deciding to post this message, because I could not find a solution for my problem. Hopefully someone will have a hint or explanation...
41
4237
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
12
2179
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
4
8878
by: Martijn Mulder | last post by:
I want my custom UserControl to size and resize with its parent, a Form. But when I use the code Size=Parent.ClientSize; in the constructor of myUserControl, Parent is null and the operation...
2
8924
by: Rene Sørensen | last post by:
In VS 2005 I'm trying to create a usercontrol, but I have some problems with it right now I can't figure out how to fix. The size of my usercontrol is "Size 574; 619" I have anchored all controls...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7383
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...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.