By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,688 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,688 IT Pros & Developers. It's quick & easy.

Positioning a control on a form?

P: n/a
Hi!

On a form I have a calendar. The form is rezised to 1024x768 (Don't
worry - this is a training case) when loaded.

Now I want to center the calendar on the form so that its edges are
equally far from the upper, right, left and bottom borders of the form.

I understand I must use the location property, am I right?

Anybody that could point me to an article on this?

Me.Name

:-)

Jan 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> I understand I must use the location property, am I right?

The Location property of a System.Windows.Forms.Control is a Point
structure. The Point has an X and a Y member. The X member defines the
distance (in pixels) of the left edge of the control from the left edge of
the client area of the Container Control it resides in. The Y member defines
the distance (in pixels) of the top edge of the control from the top edge of
the client area of the Container Control it resides in.
Now I want to center the calendar on the form so that its edges are
equally far from the upper, right, left and bottom borders of the form.
Knowing what I've just told you, it's a simple Math problem. A Control is a
rectangle that occupies a certain area in its parent rectangle. The Control
has a Width and a Height property as well. Taking each of these in turn, the
Width of the parent rectangle minus the width of the Control rectangle
yields the total amount of space available on both sides. Half of the width
of the Control rectangle defines its center. Half of the parent Control
rectangle defines the center of the parent Control rectangle. To center the
Control in the parent Control, the center of the Control must coincide with
the center of the parent Control. As the X member of the Location property
defines the distance from the left edge of the parent Control, the X member
of the Location property of the Control must be half of the width of the
Parent Container minus half the width of the Control:
Parent: 100 pixels wide
0---------------25---------------50---------------75---------------100

Control: 50 pixels wide
0---------------25---------------50

1/2 of the Parent's width = 50.
1/2 of the Control's width is 25.
50 - 25 = 25.
The X position of the Control must be 25 in order to leave the same number
of pixels on the left as on the right.

Apply the same Math to get the Y member of the Location property, and you're
done!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

<gs***@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... Hi!

On a form I have a calendar. The form is rezised to 1024x768 (Don't
worry - this is a training case) when loaded.

Now I want to center the calendar on the form so that its edges are
equally far from the upper, right, left and bottom borders of the form.

I understand I must use the location property, am I right?

Anybody that could point me to an article on this?

Me.Name

:-)

Jan 13 '06 #2

P: n/a
This is a great treatise on the math involved, but there is a much
simpler way. Put the control on your form, center it with the "Center
Control" button in the form designer, then turn OFF both the Left Anchor
and the Right Anchor properties of the control. If you want to keep the
control centered vertically as well, then center the control vertically
(with the "Center Vertically" button) and make sure both the Top Anchor
and the Bottom Anchor are turned off.

You don't need to mess with catching the form resize event or anything
like that.

-mdb

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in
news:ex**************@tk2msftngp13.phx.gbl:
I understand I must use the location property, am I right?


The Location property of a System.Windows.Forms.Control is a Point
structure. The Point has an X and a Y member. The X member defines the
distance (in pixels) of the left edge of the control from the left
edge of the client area of the Container Control it resides in. The Y
member defines the distance (in pixels) of the top edge of the control
from the top edge of the client area of the Container Control it
resides in.
Now I want to center the calendar on the form so that its edges are
equally far from the upper, right, left and bottom borders of the
form.


Knowing what I've just told you, it's a simple Math problem. A Control
is a rectangle that occupies a certain area in its parent rectangle.
The Control has a Width and a Height property as well. Taking each of
these in turn, the Width of the parent rectangle minus the width of
the Control rectangle yields the total amount of space available on
both sides. Half of the width of the Control rectangle defines its
center. Half of the parent Control rectangle defines the center of the
parent Control rectangle. To center the Control in the parent Control,
the center of the Control must coincide with the center of the parent
Control. As the X member of the Location property defines the distance
from the left edge of the parent Control, the X member of the Location
property of the Control must be half of the width of the Parent
Container minus half the width of the Control:
Parent: 100 pixels wide
0---------------25---------------50---------------75---------------100

Control: 50 pixels wide
0---------------25---------------50

1/2 of the Parent's width = 50.
1/2 of the Control's width is 25.
50 - 25 = 25.
The X position of the Control must be 25 in order to leave the same
number of pixels on the left as on the right.

Apply the same Math to get the Y member of the Location property, and
you're done!


Jan 13 '06 #3

P: n/a
Damn, I thought I was going to get to answer a question. Well done Micheal.
"Michael Bray" wrote:
This is a great treatise on the math involved, but there is a much
simpler way. Put the control on your form, center it with the "Center
Control" button in the form designer, then turn OFF both the Left Anchor
and the Right Anchor properties of the control. If you want to keep the
control centered vertically as well, then center the control vertically
(with the "Center Vertically" button) and make sure both the Top Anchor
and the Bottom Anchor are turned off.

You don't need to mess with catching the form resize event or anything
like that.

-mdb

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in
news:ex**************@tk2msftngp13.phx.gbl:
I understand I must use the location property, am I right?


The Location property of a System.Windows.Forms.Control is a Point
structure. The Point has an X and a Y member. The X member defines the
distance (in pixels) of the left edge of the control from the left
edge of the client area of the Container Control it resides in. The Y
member defines the distance (in pixels) of the top edge of the control
from the top edge of the client area of the Container Control it
resides in.
Now I want to center the calendar on the form so that its edges are
equally far from the upper, right, left and bottom borders of the
form.


Knowing what I've just told you, it's a simple Math problem. A Control
is a rectangle that occupies a certain area in its parent rectangle.
The Control has a Width and a Height property as well. Taking each of
these in turn, the Width of the parent rectangle minus the width of
the Control rectangle yields the total amount of space available on
both sides. Half of the width of the Control rectangle defines its
center. Half of the parent Control rectangle defines the center of the
parent Control rectangle. To center the Control in the parent Control,
the center of the Control must coincide with the center of the parent
Control. As the X member of the Location property defines the distance
from the left edge of the parent Control, the X member of the Location
property of the Control must be half of the width of the Parent
Container minus half the width of the Control:
Parent: 100 pixels wide
0---------------25---------------50---------------75---------------100

Control: 50 pixels wide
0---------------25---------------50

1/2 of the Parent's width = 50.
1/2 of the Control's width is 25.
50 - 25 = 25.
The X position of the Control must be 25 in order to leave the same
number of pixels on the left as on the right.

Apply the same Math to get the Y member of the Location property, and
you're done!


Jan 13 '06 #4

P: n/a
Yeah, that is easy.

However...I wanted to learn how do this mathematically, so a big thanks
to both for contributing

:-)

Me.Name

Jan 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.