我正在开发一个C#.NET自定义控件,我想在设计模式下阻止用户调整高度,同时允许他们重新调整宽度。
答案 0 :(得分:13)
我知道这个问题有点老了,但万一有人找这个,我会尽力回答:
您必须覆盖用户控件中的SetBoundsCore方法。像这样:
protected override void SetBoundsCore(
int x, int y, int width, int height, BoundsSpecified specified)
{
// EDIT: ADD AN EXTRA HEIGHT VALIDATION TO AVOID INITIALIZATION PROBLEMS
// BITWISE 'AND' OPERATION: IF ZERO THEN HEIGHT IS NOT INVOLVED IN THIS OPERATION
if ((specified & BoundsSpecified.Height) == 0 || height == DEFAULT_CONTROL_HEIGHT)
{
base.SetBoundsCore(x, y, width, DEFAULT_CONTROL_HEIGHT, specified);
}
else
{
return; // RETURN WITHOUT DOING ANY RESIZING
}
}
答案 1 :(得分:1)
您是否尝试设置MinHeight
和MaxHeight
属性?
答案 2 :(得分:0)
尝试使用“DesignMode
”属性,这表示您处于设计模式,即。来自UI设计器模式。 (见MSDN)。
public int Height()
{
get { ... }
set
{
if (this.DesignMode) return;
else this.myHeight = value;
}
}
答案 3 :(得分:0)
// Override SetBoundsCore method to set resize limits
//此代码将高度修复为20,其他属性可以更改
protected override void SetBoundsCore(int x, int y,
int width, int height, BoundsSpecified specified)
{
// Set a fixed height for the control.
base.SetBoundsCore(x, y, width, 20, specified);
}