Solved

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

Posted on 2008-10-26
10
12,970 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 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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 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 63

Expert Comment

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

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video teaches viewers about errors in exception handling.

820 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