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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mikeyman4
    New Member
    • Jan 2010
    • 9

    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.ShowDialo g();

    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.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.L ocation.X, this.Location.Y , this.Width, this.Height)

    Comment

    • mikeyman4
      New Member
      • Jan 2010
      • 9

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

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by Mikey
        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.

        Comment

        • mikeyman4
          New Member
          • Jan 2010
          • 9

          #5
          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.Mdi Parent = this;
          ArtistsForm.Sho w();

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

          this.Location = new Point(X_coordin ate, 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:

          TypeOfCommentFo rm = new TypeOfComment() ;
          TypeOfCommentFo rm.MdiParent = this.MdiParent;
          TypeOfCommentFo rm.Show();

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

          this.Location = new Point(X_coordin ate, 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:

          TypeOfCommentFo rm = new TypeOfComment() ;
          TypeOfCommentFo rm.ShowDialog() ;

          this.Location = new Point(X_coordin ate, Y_coordinate);

          When I do this, the TypeOfCommentFo rm 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.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Originally posted by Mikey
            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
            Originally posted by tlhintoq
            Code:
            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 programmaticall y set its location. This has nothing to do with the mdi parent thing that you are trying to make do the work for you.

            Code:
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Text;
            using System.Windows.Forms;
            
            namespace WindowsFormsApplication1
            {
                public partial class Form1 : Form
                {
                    // Default constructor
                    public Form1()
                    {
                        InitializeComponent();
                    }
            
                  
                    // Call to have the new form automatically center on the calling form
                    public Form1(int ParentX, int ParentY, int ParentWidth, int ParentHeight)
                    {
                        // Lets say I want this form centered on the form that created me
                        int myWidth = this.Width;
                        int myHeight = this.Height;
            
                        // My new location would be 1/2 the difference in width and height between the calling
                        // form and myself, added onto the location of the calling form
            
                        int NewX = ParentX + ((ParentWidth - myWidth) / 2);
                        int NewY = ParentY + ((ParentHeight - myHeight) / 2);
            
                        this.Location = new Point(NewX, NewY);
            
            
                        InitializeComponent();
                    }
            
                    //Button on the form to make a new form of this same kind
                    //Since it is of the same kind, of the same size, centered on the original
                    //
                    private void btnNewForm1_Click(object sender, EventArgs e)
                    {
                        Form1 myNewForm = new Form1(this.Location.X, this.Location.Y, this.Width, this.Height);
                        myNewForm.BackColor = Color.Firebrick; // Just so you can see the new one on top of the old one.
                        myNewForm.ShowDialog();
                    }
                }
            }

            Comment

            • mikeyman4
              New Member
              • Jan 2010
              • 9

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

              Code:
              Form2 myNewForm = new Form2(this.Location.X, this.Location.Y, this.Width, this.Height);
              myNewForm.MdiParent = this;
              myNewForm.Show();
              Here is all the code inside Form2:

              Code:
              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.Data;
              using System.Drawing;
              using System.Linq;
              using System.Text;
              using System.Windows.Forms;
              
              namespace WindowsFormsApplication5
              {
                  public partial class Form2 : Form
                  {
                      public Form2()
                      {
                          InitializeComponent();
                      }
              
                      // Call to have the new form automatically center on the calling form 
                      public Form2(int ParentX, int ParentY, int ParentWidth, int ParentHeight)
                      {
                          // Lets say I want this form centered on the form that created me 
                          int myWidth = this.Width;
                          int myHeight = this.Height;
              
                          // My new location would be 1/2 the difference in width and height       between the calling 
                          // form and myself, added onto the location of the calling form 
              
                          int NewX = ParentX + ((ParentWidth - myWidth) / 2);
                          int NewY = ParentY + ((ParentHeight - myHeight) / 2);
              
                          this.Location = new Point(NewX, NewY);
              
              
                          InitializeComponent();
                      } 
              
                      private void button1_Click(object sender, EventArgs e)
                      {  
                          Form2 myNewForm = new Form2(this.Location.X, this.Location.Y, this.Width, this.Height);
                          myNewForm.BackColor = Color.Firebrick; // Just so you can see the new one on top of the old one. 
                          myNewForm.ShowDialog(); 
                      }
                  }
              }
              Last edited by tlhintoq; Jan 26 '10, 08:10 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

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

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  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?

                  Comment

                  • mikeyman4
                    New Member
                    • Jan 2010
                    • 9

                    #10
                    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

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

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

                      Comment

                      • mikeyman4
                        New Member
                        • Jan 2010
                        • 9

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

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          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 "ParentCent er" then maybe there is a struggle going on for which location setting has higher priority.

                          Comment

                          • mikeyman4
                            New Member
                            • Jan 2010
                            • 9

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

                            Comment

                            • mikeyman4
                              New Member
                              • Jan 2010
                              • 9

                              #15
                              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 SystemInformati on
                              RectangleToScre en

                              I can start a new thred to see what everyone thinks is the best way to get the sizes. thanks!

                              Comment

                              Working...