Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit Windows Form to any screen resolution?

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. Thanks in advance

like image 584
shamim Avatar asked Feb 22 '11 06:02

shamim


People also ask

How do I resize a Visual Basic form to fit the screen?

Put 4 option buttons in the panel, 2 rows each. Reduce the size of the panel so that the option buttons just fit. Anchor the panel to all 4 sides of the form. Start the app and select the minimize button and the form's sides will now be accesible to shrink and stretch.

How do I fix windows form size?

Right click on your form and go to properties. Then go to Layout options,see there are a property named Size. Change it as your need as width and length wise. Also see a property named StartPosition just after the Size property.

How do I make a Windows Form full screen?

The easiest way to go full screen in an application or a game is to use the Alt + Enter keyboard shortcut. This method works for most games and apps unless they use it to enable other features. The shortcut is also used to switch from full-screen mode to windowed.


4 Answers

Can't you start maximized?

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

like image 131
ppiotrowicz Avatar answered Sep 21 '22 19:09

ppiotrowicz


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:

public MainView() {     InitializeComponent();      // StartPosition was set to FormStartPosition.Manual in the properties window.     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); } 

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.

like image 20
Olivier Jacot-Descombes Avatar answered Sep 20 '22 19:09

Olivier Jacot-Descombes


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.

like image 30
Yoko Zunna Avatar answered Sep 20 '22 19:09

Yoko Zunna


Set the form property to open in maximized state.

this.WindowState = FormWindowState.Maximized;
like image 21
Bhavik Goyal Avatar answered Sep 21 '22 19:09

Bhavik Goyal