+ Post New Thread
Results 1 to 8 of 8
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 ...
  1. #1

    Join Date
    Mar 2011
    Location
    Coventry
    Posts
    61
    Thank Post
    12
    Thanked 6 Times in 6 Posts
    Rep Power
    8

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

    Code:
    lblRoomSelect.Left = pnlRoomSelect.Left + ((pnlRoomSelect.Width - lblRoomSelect.Width) / 2)
    Can anyone see what I am doing wrong?

    Or even suggest a better way of doing this?

    Thanks in advance,

    Ash

  2. #2

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,432
    Thank Post
    1,432
    Thanked 1,160 Times in 794 Posts
    Rep Power
    705
    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:

    Code:
    lblRoomSelect.Left = pnlRoomSelect.Left + ((pnlRoomSelect.Width / 2) - (lblRoomSelect.Width / 2))
    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

  3. Thanks to LosOjos from:

    iMash (20th May 2011)

  4. #3

    Join Date
    Mar 2011
    Location
    Coventry
    Posts
    61
    Thank Post
    12
    Thanked 6 Times in 6 Posts
    Rep Power
    8
    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"

    Code:
    lblRoomSelect.Left = pnlRoomSelect.Left
    That would be where my problem is coming from.
    Last edited by iMash; 20th May 2011 at 11:38 AM. Reason: Found problem

  5. #4

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,686
    Thank Post
    333
    Thanked 513 Times in 481 Posts
    Rep Power
    178
    Quote Originally Posted by iMash View Post
    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"

    Code:
    lblRoomSelect.Left = pnlRoomSelect.Left
    That would be where my problem is coming from.
    You sure you haven't set some floating margins etc?

    Left is left on my VB :P

    Steve

  6. #5

    Join Date
    Mar 2011
    Location
    Coventry
    Posts
    61
    Thank Post
    12
    Thanked 6 Times in 6 Posts
    Rep Power
    8
    Quote Originally Posted by Steve21 View Post
    You sure you haven't set some floating margins etc?

    Left is left on my VB :P

    Steve
    Does your left mean left within a panel or a tab control?

    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.

    EDIT:

    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.

    Solved it.
    Last edited by iMash; 20th May 2011 at 12:05 PM. Reason: Solved

  7. #6

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,432
    Thank Post
    1,432
    Thanked 1,160 Times in 794 Posts
    Rep Power
    705
    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?

  8. #7

    Join Date
    Mar 2011
    Location
    Coventry
    Posts
    61
    Thank Post
    12
    Thanked 6 Times in 6 Posts
    Rep Power
    8
    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.

    Schoolboy error.

    The corrected and properly functioning line is now...........

    Code:
    lblRoomSelect.Left = (pnlRoomSelect.Width / 2) - (lblRoomSelect.Width / 2)
    Thanks for the help anyway
    Last edited by iMash; 20th May 2011 at 12:47 PM.

  9. #8
    Jamo's Avatar
    Join Date
    Jan 2009
    Posts
    1,349
    Thank Post
    66
    Thanked 175 Times in 147 Posts
    Rep Power
    60
    Hi,

    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.

    J

SHARE:
+ Post New Thread

Similar Threads

  1. Better central control of Printer installation and control
    By siuko in forum Windows Server 2000/2003
    Replies: 0
    Last Post: 9th July 2010, 02:05 PM
  2. Net Control 2
    By Bramble in forum Network and Classroom Management
    Replies: 8
    Last Post: 2nd January 2010, 04:19 PM
  3. Replies: 23
    Last Post: 3rd October 2009, 05:44 PM
  4. Centering a scrolling text box in HTML
    By PWaite in forum Web Development
    Replies: 15
    Last Post: 19th January 2009, 09:53 AM
  5. Content Control
    By kamikaze in forum Network and Classroom Management
    Replies: 4
    Last Post: 30th July 2007, 07:02 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •