Solved

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

Posted on 2008-10-26
10
12,754 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

708 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

16 Experts available now in Live!

Get 1:1 Help Now