468,505 Members | 1,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

how to position a modal form on top of a modeless form

hello. i am new to programming in C# and i'm having trouble positioning a modal form. i basically have a main form (frmFormA) set as a mdi parent, then a button is pressed on frmFormA and i open another form (frmFormB) at a particular location. frmFormB is opened up as follows:
FormB = new frmFormB();
FormB.MdiParent = this;
FormB.Show();

Now from frmFormB, a button is pressed and i want to open another form, but i want it to require the user to do something before it can close. So I am opening it like this:
FormC = new frmFormB();
FormC.ShowDialog();

When i do this, FormC doesn't go exactly on top of FormB. It is slightly higher and to the left even though i'm setting it to the same position as FormB. If i change my code to open FormC the same way as I open FormB, it places FormC where i want it.

I hope i have explained my problem clearly. If not, please let me know. I am using Visual Studio 2008 with the 3.5 framework. Can anyone tell me how to get past this? Thanks.
Jan 25 '10 #1
16 4491
tlhintoq
3,525 Expert 2GB
FormB knows its own location and size.
You could pass this information to FormC when you create it.
Inside FormC use the passed information to manually set it's location.

FormC = new frmFormB(this.Location.X, this.Location.Y, this.Width, this.Height)
Jan 25 '10 #2
thanks for your reply. I have tried this and it works fine when I use Show(), but when i change it to ShowDialog(), the position is slightly off. I'm assuming the difference is b/c when i use ShowDialog(), I can't declare a parent. So I guess the coordinates on the screen are different when a form is associated to a parent versus when it is not? Does anyone have any workarounds for this? I can use Show(), but i wanted my application to stop until it receives a response from FormB. Thanks for any help.
Jan 25 '10 #3
tlhintoq
3,525 Expert 2GB
Does anyone have any workarounds for this?
Did you try my suggestion of having the form set it's own location based on the location passed to it? If that didn't work as expected, please post the code you are using to set the location.
Jan 25 '10 #4
I just tried your suggestion which was very similar to what i was doing and it gave me the same result. Here is the actual code I'm using:

The Main form has a groupbox on it that I want to cover up when Form B is opened. I store the X and Y coordinates of the groupbox in a table so i can retrieve it in other forms. so Form B is launched using the following code in Form A:

ArtistsForm = new Artists();
ArtistsForm.MdiParent = this;
ArtistsForm.Show();

Within ArtistsForm, I am grabbing the X and Y coordinates and doing this:

this.Location = new Point(X_coordinate, Y_coordinate);

This code works great. It positions Form B right over the groupbox in Form A.

Now, from Form B I want to call another form. If I call it this way, it works great:

TypeOfCommentForm = new TypeOfComment();
TypeOfCommentForm.MdiParent = this.MdiParent;
TypeOfCommentForm.Show();

Within TypeOfCommentForm I am grabbing the X and Y coordinates and doing this:

this.Location = new Point(X_coordinate, Y_coordinate);

However, I really want to launch this from modally since i want to get the input from the user before moving on, so i try this:

TypeOfCommentForm = new TypeOfComment();
TypeOfCommentForm.ShowDialog();

this.Location = new Point(X_coordinate, Y_coordinate);

When I do this, the TypeOfCommentForm is in a slightly different location - a little higher and a little to the left even though the X and Y coordinates are the same. I really feel it has to do with Show() versus ShowDialog(), but i just can't find documentation telling me why it's different (I've spent hours searching). Thanks again for trying to help. Let me know if you need more information from me.
Jan 26 '10 #5
tlhintoq
3,525 Expert 2GB
I just tried your suggestion which was very similar to what i was doing and it gave me the same result.
I don't see anywhere in your code where you are trying to create a new form AND PASSING IT THE DETAILS OF THE OLD FORM as I suggested
Expand|Select|Wrap|Line Numbers
  1. FormC = new frmFormB(this.Location.X, this.Location.Y, this.Width, this.Height);
The form you are making would have to have a constructor that is taking the four int values and using them to programmatically set its location. This has nothing to do with the mdi parent thing that you are trying to make do the work for you.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace WindowsFormsApplication1
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         // Default constructor
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.  
  20.         // Call to have the new form automatically center on the calling form
  21.         public Form1(int ParentX, int ParentY, int ParentWidth, int ParentHeight)
  22.         {
  23.             // Lets say I want this form centered on the form that created me
  24.             int myWidth = this.Width;
  25.             int myHeight = this.Height;
  26.  
  27.             // My new location would be 1/2 the difference in width and height between the calling
  28.             // form and myself, added onto the location of the calling form
  29.  
  30.             int NewX = ParentX + ((ParentWidth - myWidth) / 2);
  31.             int NewY = ParentY + ((ParentHeight - myHeight) / 2);
  32.  
  33.             this.Location = new Point(NewX, NewY);
  34.  
  35.  
  36.             InitializeComponent();
  37.         }
  38.  
  39.         //Button on the form to make a new form of this same kind
  40.         //Since it is of the same kind, of the same size, centered on the original
  41.         //
  42.         private void btnNewForm1_Click(object sender, EventArgs e)
  43.         {
  44.             Form1 myNewForm = new Form1(this.Location.X, this.Location.Y, this.Width, this.Height);
  45.             myNewForm.BackColor = Color.Firebrick; // Just so you can see the new one on top of the old one.
  46.             myNewForm.ShowDialog();
  47.         }
  48.     }
  49. }
  50.  
Jan 26 '10 #6
hello. i have coded a new project based on my understaning of the code you provided. I may still be missing something (I apologize in advance), but it seems that the Form I'm showing with ShowDialog() is still off a bit and it seems like it is off by the border size and Titlebar size. Here is the code I used. Please let me know if I'm still missing something. Thanks again.

I coded a Parent form and added a button. When the button is clicked, here is the code I'm using.

Expand|Select|Wrap|Line Numbers
  1. Form2 myNewForm = new Form2(this.Location.X, this.Location.Y, this.Width, this.Height);
  2. myNewForm.MdiParent = this;
  3. myNewForm.Show();
Here is all the code inside Form2:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication5
  11. {
  12.     public partial class Form2 : Form
  13.     {
  14.         public Form2()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         // Call to have the new form automatically center on the calling form 
  20.         public Form2(int ParentX, int ParentY, int ParentWidth, int ParentHeight)
  21.         {
  22.             // Lets say I want this form centered on the form that created me 
  23.             int myWidth = this.Width;
  24.             int myHeight = this.Height;
  25.  
  26.             // My new location would be 1/2 the difference in width and height       between the calling 
  27.             // form and myself, added onto the location of the calling form 
  28.  
  29.             int NewX = ParentX + ((ParentWidth - myWidth) / 2);
  30.             int NewY = ParentY + ((ParentHeight - myHeight) / 2);
  31.  
  32.             this.Location = new Point(NewX, NewY);
  33.  
  34.  
  35.             InitializeComponent();
  36.         } 
  37.  
  38.         private void button1_Click(object sender, EventArgs e)
  39.         {  
  40.             Form2 myNewForm = new Form2(this.Location.X, this.Location.Y, this.Width, this.Height);
  41.             myNewForm.BackColor = Color.Firebrick; // Just so you can see the new one on top of the old one. 
  42.             myNewForm.ShowDialog(); 
  43.         }
  44.     }
  45. }
Jan 26 '10 #7
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Jan 26 '10 #8
tlhintoq
3,525 Expert 2GB
but it seems that the Form I'm showing with ShowDialog() is still off a bit and it seems like it is off by the border size and Titlebar size.
Let me be sure I understand what you are saying...
When the new form is made, it is not centered on the original form.
It seems to be off center horizonally by about 5 pixels (border size) and vertically by about 25 pixels (Titlebar size).
Does that sound right?
Can you post a screen snapshot showing both the calling form (parent) and the called form (new form) so I can see exactly what you see?
Jan 26 '10 #9
Sure. Here is the screen shot. The pixels seem to be off horizontally by about 5 pixels exactly as you said and vertically by about 31 pixels (I got the bordersize and titlebar size and kind of played with the numbers to get the two forms exactly even). Let me know if i didn't do the attachment correctly. Thanks!
Attached Files
File Type: zip WindowsForm.zip (74.6 KB, 93 views)
Jan 26 '10 #10
tlhintoq
3,525 Expert 2GB
I think the difference is that the first Form2 (not the red one) is not centered on your Form1

You could consider putting in a couple labels to show x, y, width and height on your form1 and form2 forms.
That way you can see precisely where each form is and its size.
Jan 26 '10 #11
Thank you. I will try that. Another interesting note though: In the example I created, if i change the Parent form to use ShowDialog() instead of Show(), the Form2 forms line up exactly. It seems to me that Show() and ShowDialog() interpret coordinates differently.
Jan 26 '10 #12
tlhintoq
3,525 Expert 2GB
That's weird... Then again, That's Microsoft.

Have you set the form's Start Position property to 'manual'?
If it is still set to "WindowsDefault" or "ParentCenter" then maybe there is a struggle going on for which location setting has higher priority.
Jan 26 '10 #13
yes very strange. I do have the forms set to 'manual' I even tried doing it in the code itself and that doesn't work either. I still wonder if it could be the fact that I'm launching form 2 as a child and with Show(), but when I try to launch form 3 from form 2 as ShowDialog(), i can't make it a chlld. so the differences are:

1. 1 form is a child, the other is not
2. 1 form is being invoked with Show() and the other with ShowDialog(0

I will keep working on this and post an answer if i ever figure it out. thanks again for all the help.
Jan 26 '10 #14
ok i think i have it figured out. Form 1 is a parent, Form 2 is a child of that parent, and Form 3 is NOT a child of the parent. If i was to set the X and Y axis to 0,0 when i launch Form 2 (the child form), it will position itself at the 0,0 coordinates INSIDE the parent form. When I launch Form 3 (not a child) and set its X and Y axis to 0,0, it will position itself at 0,0 based on the screen so it will overlay the parent form. So the difference between the two is the size of the Parent forms titlebar and borders. So if i can get those values, i can use them to calculate where to put Form 3 so it will be right on top of Form 2 (the child form). I have seen 2 different ways to get these values:
Using SystemInformation
RectangleToScreen

I can start a new thred to see what everyone thinks is the best way to get the sizes. thanks!
Jan 27 '10 #15
tlhintoq
3,525 Expert 2GB
Stick with
SystemInformation.FrameBorderSize.Width
and the other related functions in that namespace. That's its purpose and is aware of things like system themes etc.

And may I say "Very well done!" It's nice to see someone spend the time to experiment and go through the trial-and-error process to learn something new.
Jan 27 '10 #16
Thanks I appreciate that. I spent many, many hours trying to figure this out b4 asking for help. I have found many answers on this site over the past year. Hopefully this thread can help someone else out. Thanks again for your patience and help with this. Mike
Jan 27 '10 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Hector | last post: by
3 posts views Thread by Andrew | last post: by
6 posts views Thread by Chris Thunell | last post: by
3 posts views Thread by Tim Marsden | last post: by
3 posts views Thread by hagarwal | last post: by
4 posts views Thread by =?Utf-8?B?Z2luYWNyZXNzZQ==?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.