Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

c# center modal forms

my form launches 3 modal forms.  i'm trying to center the forms based on the parent form.  The first modal form centers based n the screen - no matter which form i launch first it's always the first one that centers based on screen and not on parent.  I have a workaround which is to first launch a blank form and then hide it, but does anyone know why this would occur?
0
pdering
Asked:
pdering
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How are you opening up the dialog?

Something like this should work:
    public partial class Form1 : Form
    {

        private void button1_Click(object sender, EventArgs e)
        {
            frmDialog dialog = new frmDialog();
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.ShowDialog(this);
        }
    }

Open in new window

0
 
AndyAinscowCommented:
You can have a look at the form properties - there should be one for the location of the form.  Select the option Center Parent and that should do what you want.
0
 
pderingAuthor Commented:
Thanks!  I was leaving off the "this".
0
 
AndyAinscowCommented:
>>Thanks!  I was leaving off the "this".

Odd.  My suggestion works with just
            frmDialog dialog = new frmDialog();
            dialog.ShowDialog();

and I have checked that from idle_mind (with a minor change)
            frmDialog dialog = new frmDialog();
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.ShowDialog();   //the 'this' is removed

and that also centers to parent
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now