Home > OS >  Set MinimumSize according the controls residing inside form
Set MinimumSize according the controls residing inside form

Time:11-04

Title of my question could be make it look like a duplicate but please read ahead as my problem is a bit different.

I am trying to replicate the minimum size functionality of some popular media players like MPC-HC or VLC where when you try to make it small the minimum size it achieves is when only MenuStrip and Player Controls are visible.

The code I've written to attain this is:

public NewMain()
{
    InitializeComponent();
    int ClientTop = RectangleToScreen(ClientRectangle).Top;
    int height = menuStrip1.Height   panel1.Height   ClientTop - Top;
    label4.Text = height.ToString();
    MinimumSize = new Size(373, height);
}

The problem is that when it runs, its not working perfectly and the menuStrip1 is still getting blocked a little at bottom from the panel1 (Docked at bottom) where the player controls will be placed.

Below is the Image of what I was able to attain with above code.

What I'm getting as an output

Next Image is what I expected:

enter image description here

Note that label on left updates when resize the form and the label on the right is the determined height via code.

My Idea was to add the difference of Form's Top and the Top of total rectangle visible on the screen i.e. the height of the title bar otherwise the resulting height will be even smaller and hide the menuStrip1 completely. I don't want to hardcode any values because it'll make the interface less adaptable to the changes that I might be doing later on.

CodePudding user response:

To correctly determine the minimum height in this case is to keep the calculations relative which can be attained by:

int height = Height - (panel1.Top - menuStrip1.Bottom);

All credit goes to Hans Passant who provided this code. I'm just posting it as an answer to mark my question solved. Thank you.

  • Related