• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1327
  • Last Modified:

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!
0
torrid333
Asked:
torrid333
  • 14
  • 14
1 Solution
 
Roshan DavisCommented:
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

0
 
torrid333Author Commented:
And put it where?  And call it from wHere?
0
 
Roshan DavisCommented:
you can put this function in Form1.cs, and call this function after InitializeCompoenent()
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
torrid333Author Commented:
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.
0
 
torrid333Author Commented:
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.
0
 
torrid333Author Commented:
I have also tried calling it as the last line of the InitComponent method and it still does not center.
0
 
Roshan DavisCommented:
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

0
 
Roshan DavisCommented:
is this working?
0
 
torrid333Author Commented:
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?
0
 
Roshan DavisCommented:
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
0
 
torrid333Author Commented:
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
0
 
torrid333Author Commented:
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
0
 
Roshan DavisCommented:
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

0
 
torrid333Author Commented:
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?
0
 
Roshan DavisCommented:
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

0
 
torrid333Author Commented:
what file do I put that class in though?
0
 
Roshan DavisCommented:
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)
                {...
                 }
       }
}
0
 
Roshan DavisCommented:
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.
0
 
torrid333Author Commented:
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.
0
 
torrid333Author Commented:
I'd prefer the GlobalFuncs approach as I then don't have to add the method to every form.
0
 
Roshan DavisCommented:
The problem was the namespace "System.Windows.Forms" was missing in the new class.
Copy the below code, I've made slight changes to work the property "Parent"
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
	/// <summary>
	/// Summary description for GobalFuncs.
	/// </summary>
	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(frm.Parent != null)
			{
				src_height = frm.Parent.Height;
				src_width = frm.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

0
 
Roshan DavisCommented:
make your to replace "YourNameSpace" with your name space that is in all your Form classes.
0
 
torrid333Author Commented:
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
0
 
Roshan DavisCommented:
you need to call like

GobalFuncs.CenterScreen  instead of just CenterScreen
0
 
Roshan DavisCommented:
hope that is working :)
0
 
torrid333Author Commented:
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!
0
 
torrid333Author Commented:
Roshmon is the best
0
 
Roshan DavisCommented:
you are welcome! :)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 14
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now