44

I work on VS 2008 with C#. This below code does not work for me. My form was designed in 1024 x 768 resolution.

Our clients laptop is in 1366 x 768 resolution. To solve this problem, I set below code in Form Load event:

this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;

but the form does not resize as per screen resolution and bottom of my form gets hidden or cut or I miss the scroll bar.

Is there any way to solve this problem? Please show me the syntax.

starball
  • 20,030
  • 7
  • 43
  • 238
shamim
  • 6,640
  • 20
  • 85
  • 151
  • Where in your code have you put these lines? Also, WorkingArea deducts the size of the task bar from the screen size, but I'm not sure if that is your intention or not. – Øyvind Bråthen Feb 22 '11 at 06:46
  • 2
    Have a look at MSDN article - Automatic Scaling in Windows Forms ( http://msdn.microsoft.com/en-us/library/ms229605.aspx ) – KV Prajapati Feb 22 '11 at 06:51
  • look at my answer at similar question http://stackoverflow.com/a/33721710/4356754 – Mohamed Ali Nov 16 '15 at 12:47

8 Answers8

34

Can't you start maximized?

Set the System.Windows.Forms.Form.WindowState property to FormWindowState.Maximized

ppiotrowicz
  • 4,464
  • 3
  • 32
  • 46
  • I already use this process but my form have so many controls ,which are not fit when changing the resolution.specially when user see on laptop – shamim Feb 22 '11 at 06:49
  • 7
    This is a problem with layout in your application. You should probably use less fixed position layouting and more of anchoring and such. – ppiotrowicz Feb 22 '11 at 06:50
  • @shamim: you were telling us your form is designed for 1024x768, but on the laptop display - which has a *higher* resolution - the controls don't fit? – Doc Brown Feb 22 '11 at 06:55
  • 1
    I think what he is talking about is docking. Create panels on your form, put controls in them, and dock them to `TOP LEFT MIDDLE RIGHT` or `BOTTOM`. Then when you maximize or enlarge the form (user/programmatic) everything will follow suit. – Andrew Grinder Nov 12 '14 at 17:54
26

If you want to set the form size programmatically, set the form's StartPosition property to Manual. Otherwise the form's own positioning and sizing algorithm will interfere with yours. This is why you are experiencing the problems mentioned in your question.

Example: Here is how I resize the form to a size half-way between its original size and the size of the screen's working area. I also center the form in the working area.

On computers with multiple monitors, the user probably expects the form to open on the same screen that the mouse pointer is on. We can get it with Screen.FromPoint(Cursor.Position).

public MainView()
{
    InitializeComponent();

    StartPosition = FormStartPosition.Manual;
    Rectangle screen = Screen.FromPoint(Cursor.Position).WorkingArea;
    int w = Width >= screen.Width ? screen.Width : (screen.Width + Width) / 2;
    int h = Height >= screen.Height ? screen.Height : (screen.Height + Height) / 2;
    Location = new Point(screen.Left + (screen.Width - w) / 2, screen.Top + (screen.Height - h) / 2);
    Size = new Size(w, h);
}

Note that setting WindowState to FormWindowState.Maximized alone does not change the size of the restored window. So the window might look good as long as it is maximized, but when restored, the window size and location can still be wrong. So I suggest setting size and location even when you intend to open the window as maximized.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
15

Probably a maximized Form helps, or you can do this manually upon form load:

Code Block

this.Location = new Point(0, 0);

this.Size = Screen.PrimaryScreen.WorkingArea.Size;

And then, play with anchoring, so the child controls inside your form automatically fit in your form's new size.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Yoko Zunna
  • 1,804
  • 14
  • 21
10

Set the form property to open in maximized state.

this.WindowState = FormWindowState.Maximized;
nawfal
  • 70,104
  • 56
  • 326
  • 368
Bhavik Goyal
  • 2,786
  • 6
  • 23
  • 42
8
int h = Screen.PrimaryScreen.WorkingArea.Height;
int w = Screen.PrimaryScreen.WorkingArea.Width;
this.ClientSize = new Size(w , h);
DatRid
  • 1,169
  • 2
  • 21
  • 46
mehrdad
  • 81
  • 1
  • 1
6

You can simply set the window state

this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
5

simply set Autoscroll = true for ur windows form.. (its not good solution but helpful)..

try for panel also(Autoscroll property = true)

teo van kot
  • 12,350
  • 10
  • 38
  • 70
K_Bhosale
  • 51
  • 1
  • 1
3

You can always tell the window to start in maximized... it should give you the same result... Like this: this.WindowState = FormWindowState.Maximized;

P.S. You could also try (and I'm not recommending this) to subtract the taskbar height.

PedroC88
  • 3,708
  • 7
  • 43
  • 77