在设计器中创建一个具有固定高度的自定义控件

Leo*_* Vo 5 .net c# designer winforms

我想创建一个自定义控件(从Control类派生),当我将此自定义控件拖到设计器中的表单时,我只能更改其宽度.此功能与单行文本框相同.

更新:我的应用程序是Windows窗体.

小智 10

请参阅http://www.windowsdevelop.com/windows-forms-general/how-to-set-that-a-control-resizes-in-width-only-9207.shtml.

您重写SetBoundsCore并定义Designer以删除顶部和底部调整大小句柄.

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ams*_*nna 8

试试这个

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);
}
Run Code Online (Sandbox Code Playgroud)

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setboundscore(VS.71).aspx