Hi,
I have a form which has the property
StartPosition set to Center Screen
It works, but if I change the property
FormBorderStyle to something, the Center Screen property stops working?
Why is this happening?
Thanks in advance
Hi,
I have a form which has the property
StartPosition set to Center Screen
It works, but if I change the property
FormBorderStyle to something, the Center Screen property stops working?
Why is this happening?
Thanks in advance
A few points to make sense of what happened and how to avoid it in future.
StartPosition is applied when a form is first shown, and Windows uses the form's size/position at that moment to compute the centered location — so if the form is resized or its Location/WindowState is already set, the designer/runtime values can cause the appearance to be off. was right that changing the border changes the non-client area and the final size used for the centering math. (learn.microsoft.com)
Things to check quickly (these are the common causes found in practice):
If the form’s size is changed during Load (AutoSize, added controls, DPI scaling, etc.) center it after layout — the Shown event is a reliable place. Example (VB.NET):
Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.CenterToScreen()
End Sub Calling CenterToScreen (or computing a location via Screen.FromControl + WorkingArea) uses the final size and will reliably re-center the window even after runtime adjustments. (stackoverflow.com)
Checklist/caveats: make sure WindowState = Normal when centering; if you target high-DPI/multiple-monitor setups, consider DPI-aware app settings because scaling can change the final size used for centering. (learn.microsoft.com)
Summary: ’s suggestion to set the property in code is good — prefer setting it after InitializeComponent or call CenterToScreen in Shown to handle border/DPI/resize effects rather than relying only on the designer.
Jump to Post— kvprajapati 1,826>FormBorderStyle to something, the Center Screen property stops working?
Please post that code here.
Jump to Post— kvprajapati 1,826Try this code,
Form2 frm = new Form2(); frm.StartPosition = FormStartPosition.CenterScreen; frm.Show();
>FormBorderStyle to something, the Center Screen property stops working?
Please post that code here.
I change these properties in the properties windows of visual studio.
The funny thing is that it will work if I put Sizeable in the FormBorderStyle.
Any other property will not make the form appear in the center of the screen.
Any ideas?
Try this code,
Form2 frm = new Form2();
frm.StartPosition = FormStartPosition.CenterScreen;
frm.Show(); Well,
It is the main form, it is the one that appears when I run the program, the other forms and dialogs appear at the center of the screen and they have propertie of the border changed, I don't know why this is not appearing at the center.
THanks
Hi,
I fixed it puting before InitializeComponente() the code you told me like this:
this.StartPosition = FormStartPosition.CenterScreen;
It is odd that changing the property from the Visual Studio graphical interface did not work.
Thanks
From MSDN Docs:
The FormBorderStyle, MinimizeBox, and MaximizeBox properties allow you to control whether the form can be minimized, maximized, or resized at run time.
The FormBorderStyle property has an affect on size and location of your form. Sizable is the default setting. Here is a link to the documentation for the property, it may give some clues as to why the form behaves this way when setting this property.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.