简体   繁体   中英

Set UserControl's max height without setting max width

I have a UserControl for a ComboBox with predefined values. I that the programmer who uses this control will be able to stretch the control horizontally, but not vertically.

Here , @SLaks suggests to set the MaximumSize property, but than, I would have to limit the width, which I don't want to.

So how to accomplish that?

You can override SetBoundsCore and make it to use height of ComboBox as height of your control:

protected override void SetBoundsCore(int x, int y, int width, int height,
    BoundsSpecified specified)
{
    base.SetBoundsCore(x, y, width, comboBox1.Height, specified);
}

Note 1: If your UserControl contains just a ComboBox , it's better to derive from ComboBox rather than creating a user control containing a ComboBox

Note 2: You can make it obvious in the designer that control can just resized from left or right by creating a new ControlDesigner and overriding SelectionRules property. To do so, add a reference to System.Design assembly and then create a custom designer:

using System.Windows.Forms.Design;
public class MyControlDesigner : ControlDesigner
{
    public override SelectionRules SelectionRules
    {
        get
        {
            return SelectionRules.LeftSizeable |
                   SelectionRules.RightSizeable |
                   SelectionRules.Moveable;
        }
    }
}

Then it's enough to decorate your custom control with designer attribute this way:

[Designer(typeof(MyControlDesigner))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM