Muhammad Faizan Khan
Muhammad Faizan Khan

Reputation: 10571

WinForm is opening in large size than screen

I have a window form and here its main property

  1. windows state to maximized
  2. Auto size true and
  3. AutoSizeMode set to GrowAndShrink

It is working fine (opening in maximized size, fit according to screen) in my PC but when i am trying this application into another PC the form size opening in large size. It is maximized but its controls are in large size. Whats i am doing wrong?

Remember there are two screens attached and I have precisely mention (in Form load event) to my form that it open in specfic screen using this code snippet.

  int displayScreen = GetScreenNumber();
  this.Location = Screen.AllScreens[displayScreen].WorkingArea.Location;

Upvotes: 1

Views: 341

Answers (1)

Suraj Kumar
Suraj Kumar

Reputation: 5653

You can set the minimum and maximum size of form as shown below

this.MinimumSize = new Size(140, 480);
this.MaximumSize = new Size(140, 480);

You can also use it as below

private void Form1_Load(object sender, EventArgs e)
        {
            int h = Screen.PrimaryScreen.WorkingArea.Height;
            int w = Screen.PrimaryScreen.WorkingArea.Width;
            this.ClientSize = new Size(w, h);
        }

Another way it can work for you is the

Rectangle screen = Screen.PrimaryScreen.WorkingArea;
int w = Width >= screen.Width ? screen.Width : (screen.Width + Width) / 2;
int h = Height >= screen.Height ? screen.Height : (screen.Height + Height) / 2;
this.Location = new Point((screen.Width - w) / 2, (screen.Height - h) / 2);
this.Size = new Size(w, h);

Upvotes: 3

Related Questions