Solved

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

Posted on 2008-10-26
10
12,860 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
  • 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 62

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
 
LVL 62

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 62

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 62

Accepted Solution

by:
Fernando Soto earned 500 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 62

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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…

864 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now