Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Perhaps the title is not very good - therefore I try to explain my "problem" :
I often create my own Controls and/or UserControls.
All of my Control are scalable.
In some of my UserControls I set the Size-Property in the Constructor-Script (New) after the InitializeComponents-Call to a fixed value (for example Mybase.size = new size (150,30)). This setting is completly ignored when I put the Control to a Form (for example). After putting it on a Form it is all times possible to resize the Control and this setting is also stored in the Designer-Script of the Form (or in a Container-Control).

Where do I make the mistake ?
Posted
Comments
Sergey Alexandrovich Kryukov 4-May-15 15:42pm    
First, which UserControl? System.Windows.Controls.UserControl, System.Windows.Forms.UserControl, System.Web.UI.UserControl? Anything else?
Always provide full type name, at least once.
—SA
Ralf Meier 4-May-15 16:01pm    
It is a System.Windows.Forms.UserControl ...
Sergey Alexandrovich Kryukov 4-May-15 17:39pm    
Then my Solution 2 will fork for you. I updated it with the code sample.
Will you accept the solution formally now.
—SA

Your question is ambiguous. I need an exact full type name to give an exact answer. However, chances are, the solution is: Size (or Width and Height) is not enough and is not even required. You can prescribe fixed size by setting the properties MinimumSize and MaximumSize to the same constant value.

[EDIT: After clarification]

All right, this is the class System.Windows.Forms.UserControl:
https://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol%28v=vs.110%29.aspx[^].

Do this, for example:
C#
public class MyClass : UserControl {

    public MyClass {
        defaultSize = //... setup some fixed size
    }
    
    protected virtual Size DefaultMaximumSize {
        get { return this.defaultSize; }
    }
    protected virtual Size DefaultMinimumSize {
        get { return this.defaultSize; }
    }

    Size defaultSize;

    // ...
    
}

—SA
 
Share this answer
 
v2
Comments
Ralf Meier 5-May-15 3:40am    
Hello Sergey,

now I am a step further with my "Problem".
At first : your solution was not helpfull (sorry because I readed many very helpfull Threads from you) because now the control is not longer resizable. That was not the Goal ...
My mistake was the Property "AutoScaleMode" which is normaly switched by me to "None". In the current control I've forgotten it. So the Size-Setting was not ignored but "autoscaled" by the Background-Functionality of the Control.

Nevertheless - thanks for your assistance ...
Sergey Alexandrovich Kryukov 5-May-15 10:16am    
All right, now I can answer your exact question: what is your mistake. You mistake was not explaining what you wanted to achieve.
—SA
Ralf Meier 5-May-15 13:13pm    
I think I have tried it.
But it is OK - my actual problem is solved ...
Sergey Alexandrovich Kryukov 5-May-15 13:42pm    
All right, good luck.
—SA
The chances are it's being overwritten in the InitializeComponent method of the parent control - in this case the Form that holds the UserControl - if you look at the form Designer code you will almost certainly see it setting the Size property of your UserControl.

You could hide the Size property by creating a new Size with Shadows[^] in the declaration to make the "genuine" Base.Size property only visible from within the class.
 
Share this answer
 
Comments
Ralf Meier 4-May-15 16:00pm    
You are quiet right - I tried it by myself with this solution and I know, that it works. But I can't understand why my first try not works.
When I walk through the new script then first the 'InitializeComponent' is called. After this I set the Size-Property to the value I wish (which is completely ignored).
When I put the Control on a Form it generates a Size-value which doesn't compare to the original Control-Size (when I designed the control) and also it doesn't compare to the wished value - but everytime it has the same value - wherever it comes ...
OriginalGriff 4-May-15 16:13pm    
Because it's down to *when* the InitialiseComponent method is called for your control and for the form that contains it. Because your control is a part of the form, it's constructor is called before the constructor for the form itself, which means that your setting of Size is overwritten by the setting from the form - as you'd expect in normal circumstances.

Try adding Console.WriteLine to see the order of execution and you'll see what I mean.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900