Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock the Height resizing in a .NET Custom Control while in design mode

I'm developing a C# .NET Custom Control and I want to prevent the user, while in design mode, from resizing the Height, while allowing them to reize the Width.

like image 566
Gio Avatar asked Dec 07 '22 02:12

Gio


2 Answers

I know this question is a little old, but just in case someone looks for this I'll try to answer it:

You have to override the SetBoundsCore method in your user control. Something like this:

protected override void SetBoundsCore(
    int x, int y, int width, int height, BoundsSpecified specified)
{
    // EDIT: ADD AN EXTRA HEIGHT VALIDATION TO AVOID INITIALIZATION PROBLEMS
    // BITWISE 'AND' OPERATION: IF ZERO THEN HEIGHT IS NOT INVOLVED IN THIS OPERATION
    if ((specified & BoundsSpecified.Height) == 0 || height == DEFAULT_CONTROL_HEIGHT)
    {
        base.SetBoundsCore(x, y, width, DEFAULT_CONTROL_HEIGHT, specified);
    }
    else
    {
        return; // RETURN WITHOUT DOING ANY RESIZING
    }
}
like image 185
Pollito Avatar answered Jan 18 '23 13:01

Pollito


Did you try to set MinHeight and MaxHeight properties?

like image 29
Fyodor Soikin Avatar answered Jan 18 '23 15:01

Fyodor Soikin