Positioning a control on a form?

  • Thread starter Thread starter gsb58
  • Start date Start date
G

gsb58

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

:-)
 
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.
 
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
 
Yeah, that is easy.

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

:-)

Me.Name
 
Back
Top