Coding Thread, VB.Net Centering A Control in Coding and Web Development; I am seriously confused as to why this isn't working.
I'm trying to create a resize sub for my form ...
20th May 2011, 11:08 AM #1
- Rep Power
VB.Net Centering A Control
I am seriously confused as to why this isn't working.
I'm trying to create a resize sub for my form so that when you resize the form, all of the content resizes and places itself in the correct position.
I have a statement that is supposed to centre a label (not resized) within a panel on resize. But it always ends up significantly further right than I calculate it should be.
The offending line of code is......
Can anyone see what I am doing wrong?
lblRoomSelect.Left = pnlRoomSelect.Left + ((pnlRoomSelect.Width - lblRoomSelect.Width) / 2)
Or even suggest a better way of doing this?
Thanks in advance,
20th May 2011, 11:14 AM #2
I suspect it's because the position refers to the top-left corner of the control, not it's centre, so you'd need to use (something like, might be dodgy code) this:
As I say, not sure the code's exactly right, but you need to take in to account the control's width as well as the width of the panel
lblRoomSelect.Left = pnlRoomSelect.Left + ((pnlRoomSelect.Width / 2) - (lblRoomSelect.Width / 2))
20th May 2011, 11:33 AM #3
- Rep Power
Thanks for the suggestion Los0jos.
I have tried it but it results in the same. I'll continue to play and see if I can find out why it is doing it.
I have just tried setting the left of the label to the left of the panel and the label ends up about 10 - 20 pixels away from the left of the panel. Grrrr why cant left actually mean left not "left plus a bit"
That would be where my problem is coming from.
lblRoomSelect.Left = pnlRoomSelect.Left
Last edited by iMash; 20th May 2011 at 11:38 AM.
Reason: Found problem
20th May 2011, 11:43 AM #4
You sure you haven't set some floating margins etc?
Originally Posted by iMash
Left is left on my VB :P
20th May 2011, 12:04 PM #5
- Rep Power
Does your left mean left within a panel or a tab control?
Originally Posted by Steve21
My left means left if I'm referencing say another textbox or another label
But all of my margins are set as 0 and all padding is set as 0 and still when I reference the left of the panel its left plus a bit.
My bad I'm just being a complete tit and forgetting that when I reference within a panel that the left of the panel is 0 regardless of what the location of the panel is in reference to everything else.
Its been a while since I wrote anything in VB.
Last edited by iMash; 20th May 2011 at 12:05 PM.
20th May 2011, 12:06 PM #6
We might need to see a bit more code...
would you be willing to share the entire chunk related to form resizing so we can see if there's anything odd going on with it?
20th May 2011, 12:13 PM #7
- Rep Power
Thanks Los0jos but I have solved it now.
In the 6 months that I haven't used Visual Studio I forgot that when you are referencing something within a panel that the top left of the panel is always 0, 0 regardless of where the panel sits within the form.
The corrected and properly functioning line is now...........
Thanks for the help anyway
lblRoomSelect.Left = (pnlRoomSelect.Width / 2) - (lblRoomSelect.Width / 2)
Last edited by iMash; 20th May 2011 at 12:47 PM.
12th June 2011, 02:22 PM #8
Just thought I would mention that its not a good way of using the controls by manually doing that.
There are dedicated position controls such as the layout panels in VB.NET which you can use to automatically position your controls. This will sort out all the postitioning for you and give you a consistent interface in which you won't have to duplicate all the code above.
By siuko in forum Windows Server 2000/2003
Last Post: 9th July 2010, 02:05 PM
By Bramble in forum Network and Classroom Management
Last Post: 2nd January 2010, 04:19 PM
By maplay in forum MIS Systems
Last Post: 3rd October 2009, 05:44 PM
By PWaite in forum Web Development
Last Post: 19th January 2009, 09:53 AM
By kamikaze in forum Network and Classroom Management
Last Post: 30th July 2007, 07:02 AM
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)