Link to home
Start Free TrialLog in
Avatar of Tim Turner
Tim TurnerFlag for United States of America

asked on

StartPosition CenterScreen does not work

Hi,
My form does not center.  I set it in the Properties and it shows up upper left.

I then set it to StartPosition Manual and tried to set X and Y to move it over and down.

This does not work either... no movement.

I then added InitForm code to calc screen size and move to different location.

None of these work and its driving me nuts. Help!
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

try this function then
private void CenterScreen(Form frm)
{
	// Get the Width and Height of the form
	int frm_width = frm.Width;
	int frm_height = frm.Height;

	//Get the Width and Height (resolution) 
	//     of the screen
	System.Windows.Forms.Screen src = System.Windows.Forms.Screen.PrimaryScreen;
	int src_height = src.Bounds.Height;
	int src_width = src.Bounds.Width;

	//put the form in the center
	frm.Left = (src_width - frm_width) / 2;
	frm.Top = (src_height - frm_height) / 2;
}

Open in new window

Avatar of Tim Turner

ASKER

And put it where?  And call it from wHere?
you can put this function in Form1.cs, and call this function after InitializeCompoenent()
I get this error: The type or namespace "Form" cannot be found.

I think InitializeComponent is called automatically... so where would we call this from?  Just at the end of the InitializeComponent method?  I have currently put it at the end of InitForm but not sure if that executes before or after InitComponent.
I changed all the form references to "this" references and got it to compile... (instead of passing in the form just refers to the form by this in the code and the method is in the forms .cs file).... Still does not work... form is not centered vertically or horizontally on the screen.
I have also tried calling it as the last line of the InitComponent method and it still does not center.
double click on the Form in the design mode to get the Form_Load event and place the function there.

i.e.
private void Form1_Load(object sender, System.EventArgs e)
{
	CenterScreen(this);
}

Open in new window

is this working?
roshmon you're cool dude....

Partial victory.... it now DOES center the form.  For some reason I was putting it into the designer .cs file instead of the form .cs file... I moved the method to the form.cs and also called it from form_load on the last line and it works... for the most part...

HOWEVER, if I resize the application background parent form.... then it stays centered on screen but not the form.... is there any way to correct that?
could you please explain little bit more about that?
or take a screenshot of your screen and attach it here as an image, so I can have a look
OK here is the screen shot of what is working.... I have my app set to full screen maximum size... and opening this Hystudy wizard is now centering which is what I want...
Hystudy-centered-fullscreen.jpg
OK so then I CLOSE that form... you can see a quick menu of shortcuts upper left....

I then reduce the overall app background to a smaller size... not full screen but reduced in size...

I then click to open this Hystudy form again...  and it stills sets it to the screen center instead of the center of the application desktop....  Is this too confusing?

I know there is a CenterParent option but I could never get that to work either.
Hystudy-notcentered-reducedscree.jpg
if you have set the Parent of that Form1 when you created, the following updtaed function would work instead of the previous CenterScreen function
private void CenterScreen(Form frm)
{
	// Get the Width and Height of the form
	int frm_width = frm.Width;
	int frm_height = frm.Height;

	//Get the Width and Height (resolution) 
	//     of the screen
	System.Windows.Forms.Screen src = System.Windows.Forms.Screen.PrimaryScreen;
	int src_height = src.Bounds.Height;
	int src_width = src.Bounds.Width;
	if(Parent != null)
	{
		src_height = Parent.Height;
		src_width = Parent.Width;
	}

	//put the form in the center
	frm.Left = (src_width - frm_width) / 2;
	frm.Top = (src_height - frm_height) / 2;
}

Open in new window

OK this works!    Now I need this across like 20 forms.... do I have to place this code inside every form?  Does it matter?  Or is there some central place I could put the code and pass in the form?

I'm more familiar with VB6 and in those projects I would have a global library of functions.  What is the equivalent in C# in VS2008?
create a static class like the following, and call GlobalFuncs.CenterScreen(.. wherever you want :)
public static class GlobalFuncs
	{
		public static void CenterScreen(Form frm)
		{
			// Get the Width and Height of the form
			int frm_width = frm.Width;
			int frm_height = frm.Height;

			//Get the Width and Height (resolution) 
			//     of the screen
			System.Windows.Forms.Screen src = System.Windows.Forms.Screen.PrimaryScreen;
			int src_height = src.Bounds.Height;
			int src_width = src.Bounds.Width;
			if(Parent != null)
			{
				src_height = Parent.Height;
				src_width = Parent.Width;
			}

			//put the form in the center
			frm.Left = (src_width - frm_width) / 2;
			frm.Top = (src_height - frm_height) / 2;
		}
		
	}

Open in new window

what file do I put that class in though?
you can put it in Form1.cs itself (next to the class Form1 - I mean after the Form1 class)

eg.

namespace YourNameSpace
{
      
      public class Form1 : System.Windows.Forms.Form
      {
            ...
        }
       
        public static class GlobalFuncs
        {
                public static void CenterScreen(Form frm)
                {...
                 }
       }
}
or a separate .cs file and then add to the project. I think the previous option which I mentioned in the previous comment is easier.
I created file called GobalFuncs.cs
and then call it with CenterScreen(this); inside the form....

I get this error: The type or namespace "Form" cannot be found.  It does not like the word Form in the declaration for some reason.
I'd prefer the GlobalFuncs approach as I then don't have to add the method to every form.
ASKER CERTIFIED SOLUTION
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
make your to replace "YourNameSpace" with your name space that is in all your Form classes.
I got over the Form namespace problem by adding:
using System;
using System.Windows.Forms;
to top of the GobalFuncs.cs file.

But now the call to CenterScreen is not recognized in the Form1.cs file....  as if it does not look for it in GlobalFuncs.cs
you need to call like

GobalFuncs.CenterScreen  instead of just CenterScreen
hope that is working :)
Yes it is!  Sorry I've been using VB6 for a long time and VS2008 for about 6 months... inherited some big projects that I did not code but need to do "fixes" on....

Thanks for all your help!
Roshmon is the best
you are welcome! :)