Results 1 to 12 of 12

Thread: [RESOLVED] Centre All Objects on Form After Resize

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Resolved [RESOLVED] Centre All Objects on Form After Resize

    Hey guys, sorry if this has already been covered in another thread but i have been searching for a good hour or so and im getting frustrated now.

    i have managed to get the form to resize according to the screen resolution but now im struggling to centre the objects on the page, any help or links to similar threads are much appreciated

    p.s. im fairly new to coding so simple answers would be a great help

    cheers in advance

    Luke

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Centre All Objects on Form After Resize

    Hi LUK3H4RR15,

    Welcome to the Forum

    On way is to fix the Controls by setting the Control's anchor.

    To do this select the Control (such as a TextBox) and then in the properties window, select Anchor, and select all four tabs (Top, Bottom, Left, Right). This will keep the Control bound to those selected as the Form is re-sized.

    Or, you can do it with code:
    Code:
            Me.TextBox1.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
    Last edited by Bruce Fox; Mar 11th, 2010 at 05:29 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Centre All Objects on Form After Resize

    thanks for the welcome and quick reply

    the group box gets resized but its contents do not move, does the anchor have to be set on all the group box contents aswell?

    anchoring to the bottom right worked fine on a cmdbutton

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Centre All Objects on Form After Resize

    The Anchor property set to all four sides will also stretch your controls which is probably not needed.
    Basically, you're operating with two rectangles - one within another (a control within a form or a form within a desktop, etc)

    To center the inner rectangle within the outer one you should take half of the outer one's width or height and subtract half of the inner one's correspondent width or height.

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Centre All Objects on Form After Resize

    Quote Originally Posted by LUK3H4RR15 View Post
    thanks for the welcome and quick reply

    the group box gets resized but its contents do not move, does the anchor have to be set on all the group box contents aswell?

    anchoring to the bottom right worked fine on a cmdbutton
    Yes, set the anchor for all controls which you want to be automatically resized/repositioned. Controls anchor w.r.t. their parent container.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Centre All Objects on Form After Resize

    when i use the anchor on cmd buttons that are not in a group box they get resized according to the screen resolution, how would i prevent them from resizing but still centering?

    cheers for your answers

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Centre All Objects on Form After Resize

    Quote Originally Posted by LUK3H4RR15 View Post
    when i use the anchor on cmd buttons that are not in a group box they get resized according to the screen resolution, how would i prevent them from resizing but still centering?

    cheers for your answers
    read my post (#4 in this thread).
    Anchor won't help you in this case, set it to None

    This will center Button1 horizontally in the form.
    Code:
    Button1.Location = New Point(Me.Width \ 2 - Button1.Width \ 2, Button1.Top)

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Centre All Objects on Form After Resize

    ScreenPrints.zip

    if i use the code you provided the buttons overlap each other in the centre, the file is just screen prints of the objects im working with, might be of some help.

    is it possible to put on big box around everything and centre that, without the border or label showing on the box?

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Centre All Objects on Form After Resize

    Take some container control (i.e. Panel) - place all your buttons on this panel.
    Then you apply my code not to the buttons but to the panel. The buttons on it will maintain their relative positions within while the Panel will be centered as I showed you.

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Centre All Objects on Form After Resize

    thanks a lot i managed to get it working great help

    just two quick things..

    1) how do you centre vertically?

    2) whats the code for exiting the application using a command button? i just use Close() but it doesn't stop debugging :S

  11. #11
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Centre All Objects on Form After Resize

    The same way.

    Every control has Top property (and Location).
    Top is the distance between the upper edge of the parent control (or a window or a screen) and the upper edge of your control while Location combines Top and Left (the horizontal distance).

    To change the vertical position of the control you use Top property and to change its horizontal position - Left property.
    Location is just a combination of these two.

    So, you need to set the Top property at the half of the height of your parent window and subtract half of the height of your panel:

    Code:
    ' Center horizontally:
    Panel1.Left = Me.Width \ 2 - Panel1.Width \ 2
    ' or
    Panel1.Location = New Point (Me.Width \ 2 - Panel1.Width \ 2, Panel1.Top) 
    
    ' Center vertically
    Panel1.Top = Me.Height \ 2 - Panel1.Height \ 2
    ' or
    Panel1.Location = New Point (Panel1.Left, Me.Height \ 2 - Panel1.Height \ 2) 
    
    ' Center both directions:
    Panel1.Location = New Point(Me.Width \ 2 - Panel1.Width \ 2 , Me.Height \ 2 - Panel1.Height \ 2)
    You should change Me if your Panel is hosted on some other control to the name of that control.

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Centre All Objects on Form After Resize

    thanks a lot you've been a great help

Posting Permissions

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



Click Here to Expand Forum to Full Width