473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 6855
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Freek te Water | last post by:
Hi, Hope no-one is offended by my probably noob question... Context: I have a web page design, which always centres in the middle of the screen (using a 100%*100% HTML-table). Now I also use...
3
by: Ryan Wade | last post by:
If the user resizes IE I want the webform controls to be automatically repositioned in the center of the form. How can I accomplish this? Ryan
4
by: gsb58 | last post by:
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...
4
by: Austin Powers | last post by:
I want to (on one line) show something like the following ------------------------------------------------------- left centered right...
7
by: TheLongshot | last post by:
I just recently converted a few pages of my application to using master pages. Problem is, in all of my content pages, the contents are centered. I can't figure out why. The markup in the...
9
by: Chris Saunders | last post by:
This is not actually a C# question but I'm not sure which newsgroup is appropriate for this question. My apologies if I'm being "off-topic". I'm writing and application that has a group box...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.