469,964 Members | 1,669 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

Keeping control centered

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

Nov 16 '05 #1
17 6648
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"No One" <no***@yahoo.com> wrote in message
news:40***************@yahoo.com...
Is there a way to keep a control centered inside a form without having
to recalculate everytime the form is resized?

Nov 16 '05 #2
On Tue, 22 Jun 2004 08:18:05 -0400, Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
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.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
>>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" <Mo************@hotmail.com> wrote in message
news:opr9zzv5hnklbvpo@morten_x.edunord...
On Tue, 22 Jun 2004 08:18:05 -0400, Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
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.
--
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4
Hi No One,

"No One" <no***@yahoo.com> wrote in message
news:40***************@yahoo.com...
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
Nov 16 '05 #5
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" <sp**@isannoying.com> wrote in message
news:#s**************@tk2msftngp13.phx.gbl...
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" <Mo************@hotmail.com> wrote in message
news:opr9zzv5hnklbvpo@morten_x.edunord...
On Tue, 22 Jun 2004 08:18:05 -0400, Nicholas Paldino [.NET/C# MVP]

<mv*@spam.guard.caspershouse.com> wrote:
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.

--
Happy coding!
Morten Wennevik [C# MVP]


Nov 16 '05 #6
well um, he said form not web page.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
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" <sp**@isannoying.com> wrote in message
news:#s**************@tk2msftngp13.phx.gbl...
>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" <Mo************@hotmail.com> wrote in message
news:opr9zzv5hnklbvpo@morten_x.edunord...
On Tue, 22 Jun 2004 08:18:05 -0400, Nicholas Paldino [.NET/C# MVP]

<mv*@spam.guard.caspershouse.com> wrote:

> 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.

--
Happy coding!
Morten Wennevik [C# MVP]



Nov 16 '05 #7
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" <sp**@isannoying.com> wrote in message
news:#H**************@TK2MSFTNGP09.phx.gbl...
well um, he said form not web page.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
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" <sp**@isannoying.com> wrote in message
news:#s**************@tk2msftngp13.phx.gbl...
>>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" <Mo************@hotmail.com> wrote in message
news:opr9zzv5hnklbvpo@morten_x.edunord...
> On Tue, 22 Jun 2004 08:18:05 -0400, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.com> wrote:
>
> > 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.
>
> --
> Happy coding!
> Morten Wennevik [C# MVP]



Nov 16 '05 #8
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 :-)

"Daniel Pratt" wrote:
Hi No One,

"No One" <no***@yahoo.com> wrote in message
news:40***************@yahoo.com...
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

Nov 16 '05 #9
>> 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" <BC*******@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
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 :-)
"Daniel Pratt" wrote:
Hi No One,

"No One" <no***@yahoo.com> wrote in message
news:40***************@yahoo.com...
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

Nov 16 '05 #10
Then you would read incorrectly. This is a Windows form.

clintonG wrote:
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" <sp**@isannoying.com> wrote in message
news:#H**************@TK2MSFTNGP09.phx.gbl...
well um, he said form not web page.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
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" <sp**@isannoying.com> wrote in message
news:#s**************@tk2msftngp13.phx.gbl...
> >>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" <Mo************@hotmail.com> wrote in message
> news:opr9zzv5hnklbvpo@morten_x.edunord...
> > On Tue, 22 Jun 2004 08:18:05 -0400, Nicholas Paldino [.NET/C# MVP]
> <mv*@spam.guard.caspershouse.com> wrote:
> >
> > > 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.
> >
> > --
> > Happy coding!
> > Morten Wennevik [C# MVP]
>
>



Nov 16 '05 #11
John Wood wrote:
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.


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.
Nov 16 '05 #12
Hi B,

"B. Chernick" <BC*******@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
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
Nov 16 '05 #13
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.)

"John Wood" wrote:
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" <BC*******@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
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 :-)

"Daniel Pratt" wrote:
Hi No One,

"No One" <no***@yahoo.com> wrote in message
news:40***************@yahoo.com...
> 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


Nov 16 '05 #14
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?

--
John Wood
EMail: first name, dot, second name at priorganize.com
"No One" <no***@yahoo.com> wrote in message
news:40***************@yahoo.com...
John Wood wrote:
>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.


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.

Nov 16 '05 #15
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.)

"Daniel Pratt" wrote:
Hi B,

"B. Chernick" <BC*******@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
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

Nov 16 '05 #16
John Wood wrote:
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.

Nov 16 '05 #17
>>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.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"No One" <no***@yahoo.com> wrote in message
news:40***************@yahoo.com...
John Wood wrote:
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.

Nov 16 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ryan Wade | last post: by
4 posts views Thread by gsb58 | last post: by
7 posts views Thread by TheLongshot | last post: by
9 posts views Thread by Chris Saunders | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.