?
Solved

How do you center a form (after runtime) C#

Posted on 2008-10-26
10
Medium Priority
?
13,202 Views
Last Modified: 2013-12-17
I have a form window I open at runtime. It does start center screen. I have some code where every time I click inside of the form, it resizes itself by 10%. When you click it for the 11th time it reverts back to it's original size. I want the form to center itself each time it is resized. I have found several examples on the net regarding how to do this, and they all look fairly similar:

int boundWidth = Screen.PrimaryScreen.Bounds.Width;
      int boundHeight = Screen.PrimaryScreen.Bounds.Height;
      int x = boundWidth - this.Width;
      int y = boundHeight - this.Height;
      this.Location = new Point(x / 2, y / 2)

However this does not work. It places the form in the upper right quadrant (but not at 0, 0). I think this may be because I am using a widescreen monitor. I am looking for a universal solution, and I am assuming there is an easy one since:

Form1.StartPosition = FormStartPosition.CenterScreen;

works fine when the form initially loads regardless of my monitor aspect ratio.
0
Comment
Question by:Pretzel_Jesus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22807886
use:

int x = boundWidth - this.Width / 2;
int y = boundHeight - this.Height / 2;
      this.Location = new Point(x, y)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22807891
sorry, your code looks ok, disregard my previous comment
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22808058
Hi Pretzel_Jesus;

The code you posted will do what you want. I believe that there is something else resetting the location of the form. Do you have any other code that locates the position of the window?

Fernandoin
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22808062
You would think I can type my own name, LOL.
0
 

Author Comment

by:Pretzel_Jesus
ID: 22808653
I dont have anything else resetting the location. I am just changing the size... and then firing off the above code. So this has nothing to do with my screen's resolution / aspect ratio?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22809394
Can you post the code that resides the Width and Height by 10%  
0
 

Author Comment

by:Pretzel_Jesus
ID: 22810245
I had to pull the code because it wouldnt work. If you could provide a solution that would center my form after a user adjusts it by dragging the form larger / smaller that would be just as good.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 22818796
Hi Pretzel_Jesus;

This works on my system.

Fernando

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private int sizeCount = 0;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // Used to re-size the form after click 11
            // Set this to the min size you want the form
            this.MinimumSize = new Size(300, 300);
        }
 
        private void Form1_Click(object sender, EventArgs e)
        {
            if (sizeCount <= 11)
            {
                // Resize the form by 10% on each click of the form for a max of 11 times
                this.Size = new Size((int)(this.Width * 1.1), (int)(this.Height * 1.1));
                sizeCount++;
            }
            else
            {
                this.Size = this.MinimumSize;
                sizeCount = 0;
            }
            // Re-center the form after adjustment of form size
            int boundWidth = Screen.PrimaryScreen.Bounds.Width;
            int boundHeight = Screen.PrimaryScreen.Bounds.Height;
            int x = boundWidth - this.Width;
            int y = boundHeight - this.Height;
            // Position the form
            this.Location = new Point(x / 2, y / 2);
 
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:Pretzel_Jesus
ID: 31510115
Thanks! I am not sure why the others solutions weren't working but this one works fine for me.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22818871
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question