1

I'm trying to simply center a panel in a form. I do this by using the simple formula of Object Location = (Container Width - Object Width) / 2. That's a simple mathematical way of moving an object to the center of its container (x-axis).

However, it isn't working properly. It's putting probably about 10 pixels of extra space on the left side and I can't figure out why. If you reduce the size of the form enough, the panel touches the right side of the form, but is still about ten pixels away from the left side.

I've checked to make sure that any margin or padding properties are set to 0, but with no luck.

Does anybody know why this is happening and how to fix it?

Here is my exact code:

Private Sub Form_Loaded(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Center Object
    pnlRadios.Location = New Point((Me.Width - pnlRadios.Width) / 2, 5)
End Sub

Update: I also tried:

pnlRadios.Location = New Point(Me.Width / 2 - pnlRadios.Width /2 , 5)

and

pnlRadios.Left = Me.Width / 2 - pnlRadios.Width / 2

and got the exact same results with every attempt.

Update 2: I was able to do a manual workaround by adding - 10 to the formula. It's centered now but it feels like I cheated and I still don't know why I had to do that in the first place, I can't figure out why there's some sort of padding on the left side.

1 Answer 1

1

Have you tried this? I beleive I had the same issue a few years ago. I think it had todo with this:

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.desktopbounds(v=vs.110).aspx

The display width, can be different from the actual width of the form... in pixels.. (maybe theming.. who knows...)

Run this code...

MsgBox("width = " & Me.Width & vbCrLf & vbCrLf & _ 
"desktopbound width = " &  Me.DisplayRectangle.Width)

Edit: As per the OP's clarification in the comments below, to get the actual "middle" of the form, without borders and decoration:

"

I don't want to include the borders in this which explains why I was having this problem. This works:

pnlRadios.Location = New Point((Me.DisplayRectangle.Width - pnlRadios.Width) / 2, 5)

"

Sign up to request clarification or add additional context in comments.

6 Comments

Interesting property to know about but I don't see any way that could affect what's happening inside the form, that can only control where the form itself is.
But it gets more interesting... looking at it again, setdesktopbounds sets the "display" width on the screen... but you can read that property using: displayrectangle.width... I have updated the question...
Ahh, very good idea. As an output I got: `width = 290 -- desktopbound width = 270"
Wel maybe that helps you out in solving this one.. :-)
Ohhh, you know what? I get it. Me.DisplayRectangle.Width is the area inside the form while Me.Width is the area of the entire form, including the borders. I don't want to include the borders in this which explains why I was having this problem. Change your answer so that it tells me to use pnlRadios.Location = New Point((Me.DisplayRectangle.Width - pnlRadios.Width) / 2, 5) and I'll accept your answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.