Centering a Rectangle on a Form (1 Viewer)

JamesJoey

Registered User.
Local time
Today, 09:31
Joined
Dec 6, 2010
Messages
608
I'm using a tabbed interface and have placed a rectangle on a form and placed command buttons inside the rectangle.

How can I center the rectangle which is about a third the size of the form??

Is there a formula I might use?
I'm not much of a mathematician.

James
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:31
Joined
Feb 19, 2013
Messages
16,611
the coordinates you need to calculate are left and top

so to centre horizontally (using the left property) you take the width of the form, subtract the width of your rectangle and divide by 2

for vertically (using the top property), you take the height of the form, subtract the height of your rectangle and divide by 2

note that if you are doing this in vba, all measurements are in twips - there are 1440 twips to an inch or 567 to a centimetre

so in your form open event you might have

me.rectcontrol.left=(insidewidth-me.rectcontrol.width)/2
 

JamesJoey

Registered User.
Local time
Today, 09:31
Joined
Dec 6, 2010
Messages
608
I used the formula but the rectangle is noticeably too far too the left and to the bottom.

It looked mot centered when I just eyed it.
 

isladogs

MVP / VIP
Local time
Today, 14:31
Joined
Jan 14, 2017
Messages
18,219
Note the comment by CJ_London about twips
there are 1440 twips to an inch or 567 to a centimetre
Did you take this into account?
 

Mark_

Longboard on the internet
Local time
Today, 06:31
Joined
Sep 12, 2017
Messages
2,111
The calculation is (Height of form - Height of rectangle) / 2

This means if your form is 3 inches tall and your rectangle is one; (3-1) / 2 = 1.

How tall is your form and how tall is the rectangle? Also, is your form resizing?
 

JamesJoey

Registered User.
Local time
Today, 09:31
Joined
Dec 6, 2010
Messages
608
I'm not using twips.

I'm taking the width of the form (13.76) and the width of the rectangle (7.35).
Subtracting them (6.41) ad dividing that by 2 (3.2).

So, the left side of the rectangle should be at 3.2?
 

Mark_

Longboard on the internet
Local time
Today, 06:31
Joined
Sep 12, 2017
Messages
2,111
Yes, but only if you are putting in the value under the control properties and not setting it in code.

Likewise if your form can grow/shrink, it won't stay centered in and of itself. So if you are moving the form around / making it a different size, expect that controls won't keep the same proportions.
 

JamesJoey

Registered User.
Local time
Today, 09:31
Joined
Dec 6, 2010
Messages
608
I just going by the values when the form is in design view.


I'm not setting any values as far as the form's size or can grow.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:31
Joined
Feb 19, 2013
Messages
16,611
I'm taking the width of the form (13.76)
the width of the form (which is what you design) is not the same as the width of the window (which is where the form is displayed i.e. what you see)

the window may be much wider or narrower that the form. Same for height. The form can be much bigger than even the monitor.

Just hiding/showing the ribbon and/or navigation pane will make a difference to the height/width of the window. Similarly showing access full screen or not.

You asked about centralising a rectangle on a form. I suspect you should be asking how to centralise a rectangle in a window.

If you are, use what I suggested at the end of my first post. And if you want it to be dynamic - i.e. stay centred if you resize the window, put it in the form resize event.

I don't use them but if you are using 2007 or later, there are also the anchor properties which may do what you want.
 

JamesJoey

Registered User.
Local time
Today, 09:31
Joined
Dec 6, 2010
Messages
608
I'm using a tabbed interface so the form spans the width of the monitor.

It doesn't need to be perfect but I measured the width of the monitor and used the formula and it looks ok now.
I eye-balled the vertical position.

Thanks much,
James
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:31
Joined
Feb 19, 2013
Messages
16,611
I'm using a tabbed interface so the form spans the width of the monitor.
to avoid confusion, please get your terminology right. the form is as wide as you set it - up to a maximum 22 inches. the window can be any width up to the width of the monitor or multiple monitors if you have that setup.

it should be

so the window spans the width of the monitor
 

Users who are viewing this thread

Top Bottom