UserControl的大小由构造函数 [英] Size of UserControl by Constructor
问题描述
也许标题不是很好 - 因此我尝试解释我的问题:
我经常创建自己的控件和/或用户控件。
全部我的控件是可扩展的。
在我的一些UserControls中,我在InitializeComponents-Call之后在Constructor-Script(New)中设置Size-Property为一个固定值(例如Mybase.size = new size (150,30))。将控件放入表单(例如)时,将完全忽略此设置。将它放在Form上后,可以随时调整Control的大小,此设置也存储在Form的Designer-Script中(或容器控件中)。
我在哪里犯这个错误?
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 ?
推荐答案
你的问题含糊不清。我需要一个确切的完整类型名称来给出确切的答案。但是,可能的解决方案是:尺寸
(或宽度
和高度
)是不够的,甚至不需要。您可以通过将属性MinimumSize
和MaximumSize
设置为相同的常量值来规定固定大小。
好吧,这是类System.Windows.Forms .UserControl
:
https://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol%28v=vs.110%29.aspx [ ^ ]。
这样做,例如:
Your question is ambiguous. I need an exact full type name to give an exact answer. However, chances are, the solution is:Size
(orWidth
andHeight
) is not enough and is not even required. You can prescribe fixed size by setting the propertiesMinimumSize
andMaximumSize
to the same constant value.
All right, this is the classSystem.Windows.Forms.UserControl
:
https://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol%28v=vs.110%29.aspx[^].
Do this, for example:
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;
// ...
}
它可能会在父控件的InitializeComponent方法中被覆盖 - 在这种情况下,持有UserControl的表单 - 如果你看一下表单设计器代码,你几乎肯定会看到它设置你的UserControl的Size属性。
你可以通过使用阴影创建新的大小来隐藏大小属性[ ^ ]在声明中使真正的Base.Size属性仅可见来自班级内。
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.
这篇关于UserControl的大小由构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!