Lock the Height resizing in a .NET Custom Control while in design mode
I'm developing a C# .NET Custom Control and I want to prevent the user开发者_高级运维, while in design mode, from resizing the Height, while allowing them to reize the Width.
I know this question is a little old, but just in case someone looks for this I'll try to answer it:
You have to override the SetBoundsCore method in your user control. Something like this:
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
}
}
Did you try to set MinHeight and MaxHeight properties?
Try using the 'DesignMode' property, this indicates that you are in design mode, i.e. from the UI designer mode. (See MSDN).
public int Height()
{
get { ... }
set
{
if (this.DesignMode) return;
else this.myHeight = value;
}
}
// Override SetBoundsCore method to set resize limits
//this code fixes the height to 20, and other attributes can be changed
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);
}
加载中,请稍侯......
精彩评论