0

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?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
  • 1
    Why not set the `MaximumSize` to `(int.MaxValue, Height)`? – Dmitry Oct 29 '17 at 20:25
  • 1
    It is almost always a better idea to derive your own class from ComboBox. You'll inherit all of its default behavior, including the way it resizes. Only use UserControl when you need to combine multiple controls. – Hans Passant Oct 30 '17 at 15:08

1 Answers1

1

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))]
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Let me know if you have any question about this post. It's the best solution that you can use for creating a fixed-height control. – Reza Aghaei Nov 24 '17 at 09:52