Keeping control centered

  • Thread starter Thread starter No One
  • Start date Start date
N

No One

Is there a way to keep a control centered inside a form without having
to recalculate everytime the form is resized?
 
No One,

Yes, there is. When the control is first placed on the form, perform
the calcs to center the control. Once you do that, set the Anchor property
so that it anchors to all sides of its container. If you are placing the
control in the container at design time, then just center the control and
set the Anchor property at design time.

Hope this helps.
 
No One,

Yes, there is. When the control is first placed on the form, perform
the calcs to center the control. Once you do that, set the Anchor property
so that it anchors to all sides of its container. If you are placing the
control in the container at design time, then just center the control and
set the Anchor property at design time.

Hope this helps.

This will have the side effect of resizing the control though. Anchoring makes sure the distance to the side is constant, and if you anchor both up and down or left and right it will resize the control. I am afraid you will have to recalculate the position in the Form's SizeChanged event.
 
This will have the side effect of resizing the control though. <<
I doubt he would want the size of the control to remain constant, and have
the margin around it growing and shrinking, that would look just weird. And,
in fact, would seem like a candidate to have a fixed dialog border.

An alternative to Nicholas' suggestion - set the Dock property of the object
to Fill, and adjust the DockPadding property of the form to set the margin
size.

--
John Wood
EMail: first name, dot, second name at priorganize.com


Morten Wennevik said:
This will have the side effect of resizing the control though. Anchoring
makes sure the distance to the side is constant, and if you anchor both up
and down or left and right it will resize the control. I am afraid you will
have to recalculate the position in the Form's SizeChanged event.
 
Hi No One,

No One said:
Is there a way to keep a control centered inside a form without having
to recalculate everytime the form is resized?

If you want the control to remain centered in the form as it resizes,
but you want the control itself to remain the same size, center the control
in the form and set the Anchor property to "None".

Regards,
Daniel
 
What am I missing here?
Why not simply use HTML <div align="center"> to center the control?

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/



John Wood said:
I doubt he would want the size of the control to remain constant, and have
the margin around it growing and shrinking, that would look just weird. And,
in fact, would seem like a candidate to have a fixed dialog border.

An alternative to Nicholas' suggestion - set the Dock property of the object
to Fill, and adjust the DockPadding property of the form to set the margin
size.

--
John Wood
EMail: first name, dot, second name at priorganize.com


Morten Wennevik said:
This will have the side effect of resizing the control though. Anchoring
makes sure the distance to the side is constant, and if you anchor both up
and down or left and right it will resize the control. I am afraid you will
have to recalculate the position in the Form's SizeChanged event.
 
well um, he said form not web page.

--
John Wood
EMail: first name, dot, second name at priorganize.com


clintonG said:
What am I missing here?
Why not simply use HTML <div align="center"> to center the control?

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/



John Wood said:
This will have the side effect of resizing the control though. <<
I doubt he would want the size of the control to remain constant, and have
the margin around it growing and shrinking, that would look just weird. And,
in fact, would seem like a candidate to have a fixed dialog border.

An alternative to Nicholas' suggestion - set the Dock property of the object
to Fill, and adjust the DockPadding property of the form to set the margin
size.
 
I read "centered inside a form" with the emphasis on 'inside'
meaning declared HTML that if and when the form is processed
on the server the response will be rendered as HTML regardless.

A span or div element should be all that is needed unless again, I am
really missing the boat. Its not like I have not actually met more than
a handful of developers who can code in their sleep but are literally
HTML clueless. I'm just asking what seemed to be obvious to me...


<%= Clinton Gallagher



John Wood said:
well um, he said form not web page.

--
John Wood
EMail: first name, dot, second name at priorganize.com


clintonG said:
What am I missing here?
Why not simply use HTML <div align="center"> to center the control?

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/



John Wood said:
This will have the side effect of resizing the control though. <<
I doubt he would want the size of the control to remain constant, and have
the margin around it growing and shrinking, that would look just weird. And,
in fact, would seem like a candidate to have a fixed dialog border.

An alternative to Nicholas' suggestion - set the Dock property of the object
to Fill, and adjust the DockPadding property of the form to set the margin
size.

--
John Wood
EMail: first name, dot, second name at priorganize.com


On Tue, 22 Jun 2004 08:18:05 -0400, Nicholas Paldino [.NET/C# MVP]

No One,

Yes, there is. When the control is first placed on the form,
perform
the calcs to center the control. Once you do that, set the Anchor
property
so that it anchors to all sides of its container. If you are placing
the
control in the container at design time, then just center the control
and
set the Anchor property at design time.

Hope this helps.



This will have the side effect of resizing the control though. Anchoring
makes sure the distance to the side is constant, and if you anchor both up
and down or left and right it will resize the control. I am afraid you will
have to recalculate the position in the Form's SizeChanged event.
 
I've been thinking about a similar problem. The problem is this: Given several controls positioned on a form, I want them to maintain their height, width and location proportional to the form, no matter what it resizes to. I take it from this thread that Anchor/dock settings are inadequate for such a task and only recalculations can keep the controls proportionally sized and positioned.

What has me doubting my memory is that I could have sworn I heard something at TechEd 2001 to the effect that there was built-in resizing. At the time this would have been a godsend given my unpleasant experiences with 3rd party resize controls. (Unfortunately I can't find my beta handouts :-)
 
I take it from this thread that Anchor/dock settings are inadequate for
such a task and only recalculations can keep the controls proportionally
sized and positioned <<

No not at all -- if you ancor in all four directions then the object will
resize according to the size of the form. The position will stay locked
though. Maybe you should describe what exactly you mean.

--
John Wood
EMail: first name, dot, second name at priorganize.com


B. Chernick said:
I've been thinking about a similar problem. The problem is this: Given
several controls positioned on a form, I want them to maintain their height,
width and location proportional to the form, no matter what it resizes to.
I take it from this thread that Anchor/dock settings are inadequate for such
a task and only recalculations can keep the controls proportionally sized
and positioned.
What has me doubting my memory is that I could have sworn I heard
something at TechEd 2001 to the effect that there was built-in resizing. At
the time this would have been a godsend given my unpleasant experiences with
3rd party resize controls. (Unfortunately I can't find my beta handouts :-)
 
John said:
I doubt he would want the size of the control to remain constant, and have
the margin around it growing and shrinking, that would look just weird. And,
in fact, would seem like a candidate to have a fixed dialog border.

I do not want the control to resize when the form does, just "re-center".
An alternative to Nicholas' suggestion - set the Dock property of the object
to Fill, and adjust the DockPadding property of the form to set the margin
size.

I do not understand this at all.
 
Then you would read incorrectly. This is a Windows form.
I read "centered inside a form" with the emphasis on 'inside'
meaning declared HTML that if and when the form is processed
on the server the response will be rendered as HTML regardless.

A span or div element should be all that is needed unless again, I am
really missing the boat. Its not like I have not actually met more than
a handful of developers who can code in their sleep but are literally
HTML clueless. I'm just asking what seemed to be obvious to me...

<%= Clinton Gallagher

John Wood said:
well um, he said form not web page.

--
John Wood
EMail: first name, dot, second name at priorganize.com


clintonG said:
What am I missing here?
Why not simply use HTML <div align="center"> to center the control?

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/



This will have the side effect of resizing the control though. <<
I doubt he would want the size of the control to remain constant, and have
the margin around it growing and shrinking, that would look just weird. And,
in fact, would seem like a candidate to have a fixed dialog border.

An alternative to Nicholas' suggestion - set the Dock property of the object
to Fill, and adjust the DockPadding property of the form to set the margin
size.

--
John Wood
EMail: first name, dot, second name at priorganize.com


On Tue, 22 Jun 2004 08:18:05 -0400, Nicholas Paldino [.NET/C# MVP]

No One,

Yes, there is. When the control is first placed on the form,
perform
the calcs to center the control. Once you do that, set the Anchor
property
so that it anchors to all sides of its container. If you are placing
the
control in the container at design time, then just center the control
and
set the Anchor property at design time.

Hope this helps.



This will have the side effect of resizing the control though. Anchoring
makes sure the distance to the side is constant, and if you anchor both up
and down or left and right it will resize the control. I am afraid you will
have to recalculate the position in the Form's SizeChanged event.
 
Hi B,

B. Chernick said:
I've been thinking about a similar problem. The problem is this: Given
several controls positioned on a form, I want them to maintain their height,
width and location proportional to the form, no matter what it resizes to.
I take it from this thread that Anchor/dock settings are inadequate for such
a task and only recalculations can keep the controls proportionally sized
and positioned.
What has me doubting my memory is that I could have sworn I heard
something at TechEd 2001 to the effect that there was built-in resizing. At
the time this would have been a godsend given my unpleasant experiences with
3rd party resize controls. (Unfortunately I can't find my beta handouts :-)
Take a look at the Control.Scale method (Form inherits from Control).

Regards,
Daniel
 
Am I missing something? As a test I have created a 400 by 400 form. On this form I placed a 100 by 100 panel at point (50,50). This panel is anchored as you suggested. Below that I put a button and code in it's on click event to double the height and width of the form.

When I click the button, the position of the panel is still (50,50). It's width is now 500 and it's height is 480. That is not proportional by any measure. (Inicidentally the button now overlaps the panel.)
 
So why not just make the border non-resizable?
Seems a bit odd to have something centered... why is the form resizable at
all in this case?
 
Ok, am I missing the point? If I simply call Scale for the entire form, everything appears to be scaled appropriately but when users scale the form in the usual method (with mouse or the maximize box) that would be useless. Are you saying I should keep track of form size and use the scale method of each control to adjust each one?

(Incidentally I think I've found a bug in the help. The sample referenced in Control.Scale has nothing to do with Scale.)
 
John said:
So why not just make the border non-resizable?
Seems a bit odd to have something centered... why is the form resizable at
all in this case?

The form is resizable programmically, not manually. As more elements are
created and placed in the form as per a configuration file, the form may need
to expand horizontally.
 
As more elements are created and placed in the form as per a configuration
file, the form may need to expand horizontally.<<

Ah.. so where does scaling come into that.
Sounds like you're just resizing the form to make space for new controls.
 
Back
Top