Center one form over another + height of title bar

  • Thread starter C-Sharper or C-Hasher, one of the two
  • Start date
C

C-Sharper or C-Hasher, one of the two

Hi Guys and Gals,

I have a couple of problems related to centering forms on top of each other.
I have a winforms app that has a status form that I want to popup in the
center of my main form when some processing is taking place. In order to do
this I need to find out the following information:
1. Where is my main form on the (current) screen in relation to the screen's
0,0 point? Once I have the location I can work out where the sub form needs
to go easily.
2. How do I set the coordinates of the sub form in relation to the current
screen's 0,0 point?
3. What is the height of the title-bar. My status form has a blank text
property and as a result no title bar. Stupidly, the .net framework then
resets the location of the controls and shifts them all up by the height of
the title bar. But it leaves the form the same size. Which means that the
form now has a big empty patch at the bottom of it. Apparently I need to
programmatically adjust the height of my form to allow for this, hence my
question.

Regards

Greg
 
P

Paul E Collins

[centring one form relative to another]

Form's StartPosition property might be useful for you. It can be set to
CenterParent or CenterScreen, among other things.
What is the height of the title-bar.

SystemInformation.CaptionHeight.

P.
 
C

C-Sharper or C-Hasher, one of the two

Thanks for the tip on CaptionHeight. It helps, but ends up trimming off more
than I want it to. The most painful thing about this is that I've created a
proof of concept app to test the various methods of centring a form and it
behaves (form size wise) correctly! I've also just replaced the form in my
other app to see if it had somehow got mucked up and what do you know, the
new form acts in the same manner as the old one! Aaaaaarrrrrgh!

Irritating doesn't begin to describe this.

Regards

Greg


Paul E Collins said:
[centring one form relative to another]

Form's StartPosition property might be useful for you. It can be set to
CenterParent or CenterScreen, among other things.
What is the height of the title-bar.

SystemInformation.CaptionHeight.

P.
 
C

C-Sharper or C-Hasher, one of the two

Hi Guys and Gals,

An update. Problem solved! Yay! It's all to do with using the
StartupPosition property. If it's set to anything other than manual it
appears that it's next to impossible to determine where the form is in
relation to the screen, so one has to position the form on the screen
completely manually. The following code will deal with that.

Screen scrCurrent = Screen.FromControl(this); //Determine which screen the
form is being displayed on
this.Left = (scrCurrent.WorkingArea.Width - this.Width)/2;
this.Top = (scrCurrent.WorkingArea.Height - this.Height)/2;

To center another form within the main form use this:
frmMyForm.Left = this.Left + ((this.Width - frmMyForm.Width)/2);
frmMyForm.Top = this.Top + ((this.Height - frmMyForm.Height)/2);

As you can see the calculations aren't hard, it's figuring out that you need
to set the StartupPosition to manual that's the big pain.

Hope this helps somebody else

Regards

Greg
 
G

Guest

How did you get the form to show no title bar? I need that for my app. I
just went to the form properties and deleted the text for "text" property,
and sure enough it was blank but the title bar was still there. I need to
get rid of it all together.
Thanks
gv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top