Hi all!

Well, this time I have a... funny problem. One that in my logic should not exist.

I'm trying a very simple thing, centering a PictureBox in the middle of my form, both horizontally and vertically.

The PictureBox is called "CP_PB_Main"

CP_PB_Main = new PictureBox();
CP_PB_Main.Height = 150;
CP_PB_Main.Width = 150;
CP_PB_Main.Left = ((ClickPic.Width / 2) - (CP_PB_Main.Width / 2));
CP_PB_Main.Top = ((ClickPic.Height / 2) - (CP_PB_Main.Height / 2));
CP_PB_Main.Image = Resource1.bird;
CP_PB_Main.SizeMode = PictureBoxSizeMode.StretchImage;
CP_PB_Main.Click += new EventHandler(CP_PB_Main_Click);
ClickPic.Controls.Add(CP_PB_Main);

Now, by my logic it should get the height of my form, which is 300 (hardcoded), divide that by 2, which comes down to 150, then get the height from the picturebox, which is 150 (hardcoded), then half that into 75, then do 150 minus 75, which equals 75. So my picturebox should be placed at 75 pixels from the top, then it should be exactly centered... However it doesn't even look close to being centered!

Am I missing something here? Am I obtaining the wrong size-property? How can this go wrong?

Any help would be greatly appreciated!

Alex

Recommended Answers

All 11 Replies

A few remarks:
First: CP_PB_Main is a bad name. You know what it means now, will you still know it in say 2 months? Give it a meaningfull name. MyMostBeautifullPictureBoxEver is even better, although...
Second:You never say anything about the variable ClickPic, so I won't either.
Third: I believe you got to take the ClientRectangle of the form into account when you want to do calculations to put an object in the center of the form.

I suspect danny is right... we don't know what ClickPic is

Here is code to center a control and a call to suspend the layout to reduce screen flicker

private void button4_Click(object sender, EventArgs e)
    {
      this.button4.SuspendLayout();
      button4.Left = (this.Width - this.button4.Width) / 2;
      button4.Top = (this.Height - this.button4.Height) / 2;
      this.button4.ResumeLayout();
    }

this.Width and Height are of course related to this.ClientRectangle!
OOPS forgot to mention the most important: this.Height is bigger than this.ClientRectangle.Height. The titlebar is not accounted for here.

commented: i learned something new :) +11

this.Width and Height are of course related to this.ClientRectangle!
OOPS forgot to mention the most important: this.Height is bigger than this.ClientRectangle.Height. The titlebar is not accounted for here.

I did not know that :)

You cannot know everything, especially if I look at myself! I am still learning every day and it's fun :)
I wonder if there is someone out there who knows about all the classes and methods in .NET?

Use padding property,

this.Padding = new Padding(100);
pictureBox1.Dock = DockStyle.Fill;

Or

pic.Left = this.Width/2 - a.Width/2;
pic.Top = this.Height/2 - a.Height/2;

You cannot know everything, especially if I look at myself! I am still learning every day and it's fun :)
I wonder if there is someone out there who knows about all the classes and methods in .NET?

The only thing that contains all of the answers to .NET is the F1 key... even then it takes him a few minutes to remember where he stuck the relevant documentation sometimes :)

commented: The F1 key, yes of course! +11

Hi all!

WOOOW!!! Those are a LOT of answers! Absolutely fantastic!

Most beautiful of all.... The correct one is among the answers too!

First some more info since I seem to have forgotten to add that; The ClickPic is a new form that it creates; CP_PB_Main comes down to CP=ClickPic, PB=PictureBox and Main is something I use when a control is the only control in the form. It isn't great but this was just to do some quick trying, I will however take the advice to heart and see if I can make myself change that convention!

Then a little background question, why is it preferable to use the SuspendLayout() ? The way I see it that change will be done in such a short period of time that it will be hardly noticeable if at all noticed, then again, I am far from being an expert so if you all say that it does help, I will add that to my list of things to remember too.

Thanks all for the help so far, you have been awesome!!


(I will leave this thread unsolved so I might have an answer to the SuspendLayout for a day, or two at most, then I will mark it solved.)

Which solution worked for you?

>>Then a little background question, why is it preferable to use the SuspendLayout() ?

Well since you're setting the .Left and the .Top separately then the control is actually moving twice -- and the reason that I put the SuspendLayout() call in my code is that I saw the control flicker. I have a 24" monitor and was running the form maximized so I could see the control rapidly moving.

Good luck coding!

Hi all!

Firstly, my answer was derived from Ddanb's this.ClientRectangle.Height however, it was called this.ClientSize.Height for me, but since I had a lot beter idea of what I was actually looking for I found it fast enough.

Second, Sknake, what you point out sounds logical, though I put the creation of the PictureBox in a this.Load event, would it still make a difference then?
I will however keep it in mind, it's always good to know beforehand what could cause a problem and to have a general direction to look at when faced with a problem. Thank you!

And thanks all!

If you're centering in the forms load event then no, it won't matter. The controls aren't visible at this point so you're good to go.

Please mark this thread as solved if you have found an answer to your original question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.