How is it possible to centre a form in C# - c#

How can you centre a form in C#, so it appears in the middle of the screen

If you're talking about where it starts, use Form.StartPosition:
form.StartPosition = FormStartPosition.CenterScreen;
If you want to do it at an arbitrary time, you'll need to work out the size of the screen, the size of the form, and calculate it yourself before setting the Location property accordingly.

Use the StartPosition property of the form, change it to CenterScreen.

In Winforms, there is a property of a Form control called StartPosition; it's an enumeration, which includes the position "CenterScreen". Use this, and your window will appear, when first created, in the middle of the monitor on which the mouse is currently located.

You can set the StartPosition property in the designer

Click your form and press F4 to show the Properties tab, then scroll down to the StartPosition property and change the value to CenterScreen.

If you want it to be centered at startup, set your form's StartPosition ( http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx ) to CenterScreen.
If you want it to be centered some other time, you have to use the SystemInformation.WorkingArea class ( http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.workingarea.aspx ) and perform arithmetic on that like so:
X = (Screen Width - Form Width) / 2
Y = (Screen Height - Form Height) / 2
And then consider what should happen if your form was too big. Setting either or both coordinates to a negative number is poor form, so don't forget that case.

Related

Is there a better way to position a secondary window's startup location relative to the main window?

I'm trying to open another window from a menu control located on my main window and I want this window's startup position to be centered relative to the main window. I have achieved this by setting the Owner property of the secondary window to my main window and setting the WindowStartupLocation property in XAML to CenterOwner, like this:
PopupWindow about = new PopupWindow();
about.Owner = Application.Current.MainWindow;
about.Show();
about.Owner = null;
The problem with this is that a window with an Owner seems to always stay on top of the Owner and whenever the Owner window is minimized, the Owned window also minimizes. To fix this I remove the Owner after the window is shown. The code seems to work fine, but it also feels a little bit like a hack.
I know there is a way to do this by setting the startup location to manual and then calculating where the window should be positioned, but I'm starting the secondary window from a popup control and I couldn't find a way to reference a window other than main. I think I could loop through the Windows collection and check a property to see if it's the window I need, but that almost seems worse than what I'm doing here.
Is there a better or more standard way of doing this?
There is no "best" way per se to do this kind of thing. It actually doesn't have any built-in function. All of the methods for doing this are kind of "hacks", you might say. A standard way of doing this is getting the coordinates of the main window and based on the about window calculate where you want its location to be. Or you can do it your way too. Here is a simple example:
int width = mainwindow.getWidth() / 2;
int height = mainWindow.getHeight() / 2;
int locationWidth = width - about.getWidth() / 2;
int locationHeight = height - about.getHeight() / 2;
// Then set it
about.Location = new Cursor(locationWidth, locationHeight);
I don't remember exactly how to set the location, because I'm not on my normal computer. But you get the idea. Also, we do width - about.getWidth() / 2, because in C# the location is set not from the center but from the top left corner. Hope it helps.

How to place a label or a button exactly in the middle of the Form?

I can't find tools or properties to place a label or a button exactly in the middle of the Form. For example, on the X axis. VS 2015.
Design time :
In my VisualStudio2010 I have these 2 buttons to center horizontally and vertically:
Its located in the toolbar "Layout". If it isn't, you can add them by clicking the small button to the right. It is also in the Format menu.
To keep centered at Runtime: Turn off all anchoring.
Note:This will keep the control at its relative position as long as it doesn't change it Size. If it does, like autosize Labels are prone to, you will have to code the Resize event. Examples are here
For controls that may change in size, you need to catch the Resize event.
In my case I have a Panel, representing a page, inside another Panel which is the workspace. The workspace is set to autoscroll. In this scenario, it's important that the control is only centered when smaller than the container.
Whenever the form changes size or when I change the content, I call this function:
private void resetPagePos()
{
int wWS = pnlWorkspace.Width;
int hWS = pnlWorkspace.Height;
int wPage = pnlPage.Width;
int hPage = pnlPage.Height;
pnlPage.Location = new Point(Math.Max(0, (wWS - wPage) / 2), pnlPage.Top = Math.Max(0, (hWS - hPage) / 2));
}
The use of Math.Max(0, ...) makes sure that if the item doesn't fit, and the scrollbars are activates, then our page scrolls correctly. If the Left or Top are set to a negative number, you would get unwanted side-effects.

How to determine if control is outside of Form?

I am trying to determine if a dynamically added control is outside of the form.
At first, I thought it might be possible to calculate it by getting the height of the form, and location of the dynamically added control.
But I noticed that the Control.Location and Form.Height have "nothing" in common.
I don't think I really understand what the correlation is between Height and Location.
For example:
I thought that if your form has a height of 500, and I put the control at the bottom of the form, it should give the Location: X, 500 (X is width, not relevant here). But this is not correct, it shows me for example: X, 465. Am I missing something?
So I need to be able to recognize if the control is outside of the form, even if it's just one pixel.
I've found several similar questions here on SO, yet this does not really give me the answer that I need, unfortunately.
So, is there any way to do this? Is it possible to calculate it?
The Height of the form also includes the height of the title bar and borders.
You can use the ClientSize of the form:
From the documentation on MSDN:
The size of the client area of the form is the size of the form excluding the borders and the title bar. The client area of a form is the area within a form where controls can be placed. You can use this property to get the proper dimensions when performing graphics operations or when sizing and positioning controls on the form. To get the size of the entire form, use the Size property or use the individual properties Height and Width.
The position of the control is relative to its container, so (0,0) is the left upper corner inside the form.
I know this is an older thread, but you can try using this method:
public static bool IsOutofBounds(Form form, Control control)
{
int controlEnd_X = control.Location.X + control.ClientSize.Width;
int controlEnd_Y = control.Location.Y + control.ClientSize.Height;
if (form.ClientSize.Width < controlEnd_X || form.ClientSize.Height < controlEnd_Y)
{
return true;
}
else
{
return false;
}
}
It works for checking whether a control is out of bounds of its parent form.
You could use this code to check if controls is inside form:
var Inside = frm.ClientRectange.Intersect(ctrl.Bounds) == ctrl.Bounds;
the top left corner of a form is (0,0) lower right corner is (formHeight, fromWidth).
to check this place two text boxes on a form and write this code in the mouse move event to see how x and y change.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
Note that there is an difference between the number returned from the edge of the form and the size chosen by you. In my 500*500 form it is actually 460*483. the difference is always the same for any border style and any resolution.
To place a control on your form use the location structure in the form or use the top and left properties for the control; top = x, left = y.
Remember your offset from the actual height and width you measured and the dimension of the control.
To add a button with the following dimensions 80*30 in the bottom right corner I would right something like this:
button1.Location = new System.Drawing.Point(402, 430);
bottom left corner:
button1.Location = new System.Drawing.Point(0, 430);

C# How to Position a Parent Form using Client Coordinates?

I have a main form (application's parent form) which is set to the back using p/Invoke SetWindowPos. SetWindowPos asks for the X,Y coordinates of the window which are to be in Client Coordinates.
When I start the program the window is displayed center of screen. I then move the window and save the new location to Properties.Settings.Default.frmMainLocation which is bound to the frmMain.Location property.
I stop the program and then restart it - and the form is back to the original center position.
How would I set SetWindowPos to locate the window at X, Y Coordinates which are derived from the form's Location.X, Location.Y coordinates?
If you take your properties and save them to a point, you can set the window location like this:
Point pos = new Point
(
Properties.Settings.Default.posX,
Properties.Settings.Default.posY
);
this.Location = pos;
I set this in my form's Form_Load() method for the Form.Load() event with a few different sets of simple coords and it changed the position that the form was displayed on load each time. It's just a hop, skip, and a jump to have it load the coords from Properties.Settings.Default as I've shown in the example.
Actually it was a bit of a mess getting it to work.
When the App starts you need to SetWindowPos without the SWP_NOMOVE flag
Then SetWindowPos with the SWP_MOVE Flag set
Then when you want to change the position repeat the App start process.

Winform size and location of winform on screen regardless of resolution

I have a winform (c#, let's say 250px by 250px) that needs to stay in one location on the screen regardless of screen resolution i.e 800x600, 1920x1080 etc. The Winform itself contains only one element - a picturebox so what's inside really doesn't matter (no need to worry about fonts, etc.). I just need it to stick in one place on the screen from one monitor to another.
Any ideas? Thanks in advance.
Could you use one of these?
1) WindowsState = Maximized (then you dont worry, it always takes whole screen)
2) StartPosition = CenterScreen (then it always shows as centered), or CenterParent to center within parent form
3) Location = in this case you would have to do some math to get screen size, your form size than based on that center it but I dont see point of using this considering that StartPosition does it already for you.
Hope that helps
Ok, so in order to get the window at a fixed location given in % of the screen size, you need the screen size (e.g. using this answer), compute the desired position, and set it as the window-location.
Since you need to do it at startup, you could do it before you show the window, or maybe best inside a Frame.Loaded event handler.

Categories

Resources