不允许用户垂直调整 TextBox
控件的大小。 TextBox
的高度被锁定为文本框应有的理想高度。
此外,Visual Studio 甚至不为您提供垂直拖动 handle :
如何在我的UserControl
上提供相同的机制?
最佳答案
我将详细阐述汉斯的评论。您可以将专用代码(称为设计器)与 UserControl 相关联,这样当将其放置在 Visual Studio 中的窗体上时,用户配置您的控件的方式就会受到限制。
在项目中添加对
System.Design
的引用。使用以下示例代码:
[Designer(typeof(FixedHeightUserControlDesigner))] public partial class FixedHeightUserControl : UserControl { private const int FIXED_HEIGHT = 25; protected override void OnSizeChanged(EventArgs e) { if (this.Size.Height != FIXED_HEIGHT) this.Size = new Size(this.Size.Width, FIXED_HEIGHT); base.OnSizeChanged(e); } public FixedHeightUserControl() { InitializeComponent(); this.Height = FIXED_HEIGHT; } } public class FixedHeightUserControlDesigner : ParentControlDesigner { private static string[] _propsToRemove = new string[] { "Height", "Size" }; public override SelectionRules SelectionRules { get { return SelectionRules.LeftSizeable | SelectionRules.RightSizeable | SelectionRules.Moveable; } } protected override void PreFilterProperties(System.Collections.IDictionary properties) { base.PreFilterProperties(properties); foreach (string p in _propsToRemove) if (properties.Contains(p)) properties.Remove(p); } }
关于winforms - 如何让UserControl锁定到一定高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8348345/