Create a custom control which has a fixed height in designer

谁说胖子不能爱 提交于 2021-01-02 07:45:24

问题


I want to create a custom control (derived from Control class), and when I drag this custom control to a form in designer, I can only change its width. This feature is same as single-line textbox.

Update: My application is Windows Form.


回答1:


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

You override SetBoundsCore and define a Designer to remove top and bottom resize handles.

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;
            }
        }
    }
}



回答2:


Try this

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




回答3:


    this.MaximumSize = new System.Drawing.Size(0, 20);
    this.MinimumSize = new System.Drawing.Size(0, 20);

Apparently .NET assumes a minimum and maximum width of 0 to be "any width".



来源:https://stackoverflow.com/questions/2582718/create-a-custom-control-which-has-a-fixed-height-in-designer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!