Solved

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

Posted on 2008-10-26
10
12,908 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

773 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