我希望创建一个自定义控件(派生于Control类),当我将这个自定义控件拖到设计器中的表单时,我只能更改它的宽度。这个特性和单行文本框相同。
更新:我的应用程序是Windows Form。
你可以重写SetBoundsCore方法并定义一个设计器来删除控件的上下调整大小手柄。
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace MyControlProject
{
[Designer(typeof(MyControlDesigner))]
public class MyControl : Control
{
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
height = 50;
base.SetBoundsCore(x, y, width, height, specified);
}
}
internal class MyControlDesigner : ControlDesigner
{
MyControlDesigner()
{
base.AutoResizeHandles = true;
}
public override SelectionRules SelectionRules
{
get
{
return SelectionRules.LeftSizeable | SelectionRules.RightSizeable | SelectionRules.Moveable;
}
}
}
}
试试这个
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, 75, specified);
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setboundscore(VS.71).aspx
base.SetBoundsCore(x, y, width, 75, BoundsSpecified.Height);
来源:MSDN文档。 - Matheus Rocha this.MaximumSize = new System.Drawing.Size(0, 20);
this.MinimumSize = new System.Drawing.Size(0, 20);
显然,.NET假定最小宽度和最大宽度为0时为"任意宽度"。
ControlDesigner
类。这个类可用的最低.NET Framework版本是多少? - Matheus Rocha