创建一个在设计器中具有固定高度的自定义控件 [英] Create a custom control which has a fixed height in designer

查看:52
本文介绍了创建一个在设计器中具有固定高度的自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

Update: My application is Windows Form.

推荐答案

请参见您将覆盖SetBoundsCore并定义一个Designer来删除顶部和底部的调整大小手柄.

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

这篇关于创建一个在设计器中具有固定高度的自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆