How to fit Windows Form to any screen resolution? - c#

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.

Can't you start maximized?
Set the System.Windows.Forms.Form.WindowState property to FormWindowState.Maximized

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.

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.

Set the form property to open in maximized state.
this.WindowState = FormWindowState.Maximized;

int h = Screen.PrimaryScreen.WorkingArea.Height;
int w = Screen.PrimaryScreen.WorkingArea.Width;
this.ClientSize = new Size(w , h);

You can simply set the window state
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

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

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.

Related

Form opening on top-left of screen

I have a winform that I would like to open on the centre of the parent form, which is already a mdiChild (i.e. I cannot set is mdiContainer on the parent). Below is the code I use. The form I create always opens on the top-left corner of whichever parent I assign to it, which is frustrating...
loadingCircle = new Loading(Title);
loadingCircle.TopLevel = false;
loadingCircle.Parent = this;
loadingCircle.Show();
loadingCircle.BringToFront();
I have got the StartPosition switched to CenterParent in the designer, however it does not seem to do anything...
Am I missing something obvious?
To get to the center of the screen,
You can use either :
loadingCircle.StartPosition = FormStartPosition.CenterScreen;
Or :
loadingCircle.ShowDialog();
or try this code to find center position:
Form loadingCircle = new frmLoading();
loadingCircle.StartPosition = FormStartPosition.Manual;
loadingCircle.Location = new Point(this.Location.X + (this.Width - loadingCircle.Width) / 2, this.Location.Y + (this.Height - loadingCircle.Height) / 2);
loadingCircle.Show(this);

MS visual Studio Forms application : Can't get form to have less width then 100 Pixels

First question here so if I can improve something in anyway please let me know!
I am currently making a "multi-form" application.
It currently consists out of a launcher bar with various buttons and the launcher bar has a width of 150 pixels (This one is fine).
When the user presses a button another panel will open 10 pixels next to the the first panel with a width of 75. (I wanted to add the current buttons "subcategories" here)
But when calling the second form it keeps setting itself to 100 pixels (Well I think it is 100 pixels since it seems about 2/3 of the first panel)
private void button_click(object sender, EventArgs e)
{
if (activated == 0)
{
var new_y = new form2();
new_y.AutoSize = false;
new_y.Width = 75;
new_y.Height = this.Height;
new_y.Show();
activated = 1;
}
}
I stripped it of some additional code (positioning stuff) so if that could cause any problems please let me know.
But my question : How do I prevent the form setting itself to 100 pixels in width and make it the 75 pixels width I want it to be?
Thanks in advance!
Ps . FormBorderStyle is set to none
Try setting the MinimumSize property:
new_y.Width = 75;
new_y.Height = this.Height;
new_y.MinimumSize = new Size(75, this.Height);

c# winform screen resolution

I have a C# WinForms application and when I give the executable to different users the application displays in different sizes (based on their screen resolution). Some of the parts of the application can't be seen.
how can I set absolute 1280X800 for my forms and make sure that the form size will not be changed whatever resolution is!
You can use Control.ScaleControl and Control.Scale
private void MainForm_Load( object sender, EventArgs e )
{
float width_ratio = (Screen.PrimaryScreen.Bounds.Width / 1280);
float heigh_ratio = (Screen.PrimaryScreen.Bounds.Height / 800f);
SizeF scale = new SizeF(width_ratio, heigh_ratio);
this.Scale(scale);
//And for font size
foreach (Control control in this.Controls)
{
control.Font = new Font("Microsoft Sans Serif", c.Font.SizeInPoints * heigh_ratio * width_ratio);
}
}
Hope this helps.
Use the MaximumSize property of the form.
form.MaximumSize = new Size(1280, 800);
You can also set a MinimumSize if you don't want the user to make it smaller than a desired size.
You can instead design the GUI so it scrolls up and down more easily.You can make use of the following
Layout Managers
Docking
Anchors
The property
Screen.PrimaryScreen.WorkingArea
is very useful for form sizing and positioning. For example this code:
this.Width = Screen.PrimaryScreen.WorkingArea.Width/2;
this.Height = Screen.PrimaryScreen.WorkingArea.Height/2;
this.Top = (Screen.PrimaryScreen.WorkingArea.Top + Screen.PrimaryScreen.WorkingArea.Height)/4;
this.Left = (Screen.PrimaryScreen.WorkingArea.Left + Screen.PrimaryScreen.WorkingArea.Width)/4;
will place the form in which it is executed in the middle of the screen and size it to half the screen.
The WorkingArea var is used to exclude stuff like the task bar and other docked items on the desktop when calculating the size of the screen.
Hope this helps.

Set window's default location on a user screen

I have a windows Form Application.I want that when the application will run my window will always just on Taskbar and on the Right side means on the bottom-right of a user screen.It doesn't matter what is a user screen resolution is.So how can i do that ???
To build on MeNoMore's answer, to make your form show up in the bottom right corner of the primary screen (the screen with the task bar is usually the primary screen) you would set the following in the constructor of your form.
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width,
Screen.PrimaryScreen.WorkingArea.Height - this.Height);
Other answers will not work. You have to do two things:
Set form's StartPosition to Manual
Set form's Location to what point you want
E.g. inside form's constructor:
StartPosition = FormStartPosition.Manual;
Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width,
Screen.PrimaryScreen.WorkingArea.Height - Height);
You can try to use:
Form1.Location = new Point(x, y);
Take a look here
Use Form.StartPosition property
You use the WinTaskBar component for considering other factors on a desktop/screen.
[DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]
static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);
It will let you control the positions (left & top).
Example:
formStartLeft = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
formStartTop = taskBarHeight + Screen.PrimaryScreen.WorkingArea.Height;

How to auto resize and adjust Form controls with change in resolution

I have noticed that some applications change their controls' positions to fit themselves as much as possible in the current resolution. For example, if the window is maximized, the controls are set in such a way that the overall GUI looks balanced.
Is it possible to make or implement this functionality in Visual studio 2010 using C#?
Use Dock and Anchor properties. Here is a good article. Note that these will handle changes when maximizing/minimizing. That is a little different that if the screen resolution changes, but it will be along the same idea.
Use combinations of these to get the desired result:
Set Anchor property to None, the controls will not be resized, they only shift their position.
Set Anchor property to Top+Bottom+Left+Right, the controls will be resized but they don't change their position.
Set the Minimum Size of the form to a proper value.
Set Dock property.
Use Form Resize event to change whatever you want
I don't know how font size (label, textbox, combobox, etc.) will be affected in (1) - (4), but it can be controlled in (5).
float widthRatio = Screen.PrimaryScreen.Bounds.Width / 1280;
float heightRatio = Screen.PrimaryScreen.Bounds.Height / 800f;
SizeF scale = new SizeF(widthRatio, heightRatio);
this.Scale(scale);
foreach (Control control in this.Controls)
{
control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
..and to detect a change in resolution to handle it (once you're using Docking and Anchoring like SwDevMan81 suggested) use the SystemEvents.DisplaySettingsChanged event in Microsoft.Win32.
sorry I saw the question late,
Here is an easy programmatically solution that works well on me,
Create those global variables:
float firstWidth;
float firstHeight;
after on load, fill those variables;
firstWidth = this.Size.Width;
firstHeight = this.Size.Height;
then select your form and put these code to your form's SizeChange event;
private void AnaMenu_SizeChanged(object sender, EventArgs e)
{
float size1 = this.Size.Width / firstWidth;
float size2 = this.Size.Height / firstHeight;
SizeF scale = new SizeF(size1, size2);
firstWidth = this.Size.Width;
firstHeight = this.Size.Height;
foreach (Control control in this.Controls)
{
control.Font = new Font(control.Font.FontFamily, control.Font.Size* ((size1+ size2)/2));
control.Scale(scale);
}
}
I hope this helps, it works perfect on my projects.
Here I like to use https://www.netresize.net/index.php?c=3a&id=11#buyopt. But it is paid version.
You also can get their source codes if you buy 1 Site License (Unlimited Developers).
How ever I am finding the nuget package solution.
add this code at page load do for all control or add all control in containers
int x;
Point pt = new Point();
x = Screen.PrimaryScreen.WorkingArea.Width - 1024;
x = x / 2;
pt.Y = groupBox1.Location.Y + 50;
pt.X = groupBox1.Location.X + x;
groupBox1.Location = pt;
in the form load event add this line
this.WindowState = FormWindowState.Maximized;
private void MainForm_Load( object sender, EventArgs e )
{
this.Size = Screen.PrimaryScreen.WorkingArea.Size
}
this.WindowState = FormWindowState.Maximized;

Categories