|
-
Mar 11th, 2010, 05:01 PM
#1
Thread Starter
New Member
-
Mar 11th, 2010, 05:22 PM
#2
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.
-
Mar 11th, 2010, 05:44 PM
#3
Thread Starter
New Member
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
-
Mar 11th, 2010, 05:46 PM
#4
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.
-
Mar 11th, 2010, 05:47 PM
#5
Re: Centre All Objects on Form After Resize
 Originally Posted by LUK3H4RR15
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.
-
Mar 11th, 2010, 06:00 PM
#6
Thread Starter
New Member
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
-
Mar 11th, 2010, 06:11 PM
#7
Re: Centre All Objects on Form After Resize
 Originally Posted by LUK3H4RR15
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)
-
Mar 11th, 2010, 06:23 PM
#8
Thread Starter
New Member
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?
-
Mar 11th, 2010, 06:34 PM
#9
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.
-
Mar 11th, 2010, 06:37 PM
#10
Thread Starter
New Member
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
-
Mar 11th, 2010, 06:46 PM
#11
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.
-
Mar 11th, 2010, 06:55 PM
#12
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|