Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi i am working on window appication project in visual studio-10...My form was designed in 1600 x 900 resolution.

My clients using different resolution. To solve this problem, i tried below code in load page...but its not working properly....if possible plz tell me the code to solve....Thanks in advance

C#
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
Posted
Updated 23-Jul-20 0:20am

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

this line can only modify size of window-form (it will not effect controls' locations & size)

for fit your form in any window use
C#
this.windowstate = maximized


and for controls you should use anchor & dock property
or you can also calculate & set the location & size of controls run time.

Happy Coding!
:)
 
Share this answer
 
v2
Comments
ANOOP CL NAIR 26-Jul-12 6:56am    
thank you for your nice comment...
Member 13006928 17-Feb-17 4:00am    
I try to use that code but it is only working in my primary screen when i drag my form to my secondary screen its not working
Docking can help of course. Just look in the Properties of the control how you want to be resolution independent and set the docking, then the control automatically resizes too when the form does.
For more please see this CP article;
Anchoring, Docking Properties of Controls in Windows Forms:
Working with Anchoring and Docking Properties[^]

Anchor and Dock Child Controls[^]
Anchor and Dock Child Controls[^]

Also have look on similar discussions, you will get surely needful from them:
Zom Out malfunctions when Screen resolution changes[^]
GUI Apperance - C#.Net[^]
how to dock button so that it can adjust with the form[^]
 
Share this answer
 
Comments
ANOOP CL NAIR 26-Jul-12 8:58am    
thank you for your nice comment
If you are using Winforms, then the chances are you can't do it with any real success - you can in WPF because it is very different.

However, in Winforms, just changing the size of a total form does not automatically affect the size of it's content - it can, if you have the Anchor and Dock properties set correctly, but even then, it doesn't affect the size of text in buttons, labels and so forth, so the resulting form can look very rubbish.

There is no real short cut. The best solution is to design the form to work with a minimum resolution, and allow teh user to scale up from that, rather than start with a maximum resolution and try to scale down.

Sorry, but I think you will have to do a fair amount of work!
 
Share this answer
 
Comments
ANOOP CL NAIR 26-Jul-12 6:55am    
thank you for your nice comment...
OriginalGriff 26-Jul-12 6:59am    
You're welcome!
Use the Scale method in the form load event. You will need to know the screen resolution of the computer you designed the form on. In your example, it was 1600 x 900. I set the initial resolution in a constant or a global config, but I used literal values here to keep the code simple. This will size the form and all child objects.

private void FormAutoResolution_Load(object sender, EventArgs e)
{
    // Scale our form to look like it did when we designed it.
    // This adjusts between the screen resolution of the design computer and the workstation.
    int ourScreenWidth = Screen.FromControl(this).WorkingArea.Width;
    int ourScreenHeight = Screen.FromControl(this).WorkingArea.Height;
    float scaleFactorWidth = (float)ourScreenWidth / 1600f;
    float scaleFactorHeigth = (float)ourScreenHeight / 900f;
    SizeF scaleFactor = new SizeF(scaleFactorWidth, scaleFactorHeigth);
    Scale(scaleFactor);

    // If you want to center the resized screen.
    CenterToScreen();
}
 
Share this answer
 
Comments
Member 14130040 3-Aug-21 17:11pm    
Works great but what about font size, that doesn't seem to be adjusting.
I suggest not to use Dock and Anchor properties. They are nightmarish.
1. Just leave the Anchor with top and left highlighted.
2. Get before screen size measurements by the following method:
AnteWidth = Me.Width and AnteHeight = Me.Height
You need to declare AnteWidth, and Anteheight or any other names as variables.
3.Get the current screen height and width measurements using the following method:
Dim screenWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width
Dim screenHeight As Integer = Screen.PrimaryScreen.WorkingArea.Height
4. Calculate the width and height ratios as follows:
WidthRatio = screenWidth/AnteWidth and HeightRatio = screenHeight/AnteHeight

In the next step multiply the width and height measurement of each control with respective ratios.
Example: Button1.Width = Button1.Width * WidthRatio
Button1.Height = Button1.Height * HeightRatio
You need to assign new locations also to the controls that are next to the left most control. You will figure it out intuitively.
All this code must be entered in the form load area after keeping the form size to the minimum in order for the new display to occupy the screen completely. It is tedious but it works perfect.
 
Share this answer
 
Comments
Dave Kreskowiak 31-Jan-19 20:01pm    
First, this was asked and answered seven years ago.

Next, Anchor, Dock, TableLayoutPanel, FlowLayoutPanel, and other controls and properties may be a "nightmare" for you, but not for other people. I loathe writing code for situations where I don't have to. I have no problems with using the controls properly for varying resolutions and don't have to write a single line of code to do it.
CHill60 1-Feb-19 4:58am    
"I suggest not to use Dock and Anchor properties" - Unfortunately your advice is contrary to just about every other experienced developer in the Windows stack!
Your approach was fine in VB6 which went out of support in 2005, so you are only about 13 years out of date.
Programming is about exploiting the tools that are available to get the job done, and modern language development is about giving developers those tools. My advice would be to get up to speed with new developments in your chosen language(s) (or learn new ones), otherwise you could find yourself unemployable. I've seen it happen.
You Can Use like this :-


C#
this.windowstate = maximized
 
Share this answer
 
Comments
ANOOP CL NAIR 26-Jul-12 8:58am    
thank you for your nice comment
Yatin chauhan 26-Jul-12 10:12am    
Always Welcome Dear..........!!!!!!!!!
Use tablelayout control. It works for me.
 
Share this answer
 
Comments
CHill60 9-Nov-18 4:45am    
"It works for me" .. well done you! However, there is no such control (it's a TableLayoutPanel control). It's also not really relevent to screen resolution either, you would still need to use Docking and Anchoring as described in the other solutions from 2012. Plus going by the Microsoft documentation, it would not be appropriate in this case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900