Solved

Centering a form using parent?

Posted on 2006-06-19
8
466 Views
Last Modified: 2009-12-16
Hi there,

I am having a problem centering a winform on its parent...

I set the proeprty StartPosition to CenterParent

and then using this code ... it doesn't work... it gives me an error msg.

                        Test frmTest = new Test();
                        frmTest .Parent = this;
                        frmTest .Show();

Error is : cannot add a top level control to a control

It is something to do with frmTest.Parent .... but i need to set the Parent

Any ideas, i know its something simple?

Thanks in advance

Ian
0
Comment
Question by:ianinspain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16935950
                  Test frmTest = new Test();
                    frmTest .Show(this);
0
 

Author Comment

by:ianinspain
ID: 16940211
this is what i originally thought it was too, i remember in VB days it was...

but i tried it... and i get this

No overload for method 'Show' takes '1' arguments

I remember in old vb days, you used to just say show(the parent) .... but doing this in c#

Test frmTest = new Test();
frmTest .Show(this);

Maybe i am missing something...???

Thanks

Ian
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16940261
I see, this method if supported only in .NET 2.0. I have no solution except setting StartPosition to Manual and calculating Left, Top in the Form_Load event handler. May be there is something better, I don't know.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16944629
This would work, but would create a modal form:
  Test frmTest = new Test();
  frmTest.ShowDialog(this);

If you want a non-modal form, you could do this:

  Test frmTest = new Test();
  frmTest.Owner = this;
  frmTest.Show();

Bob


0
 
LVL 1

Expert Comment

by:winsoftech
ID: 16957304
  Test frmTest = new Test();
   frmTest .Parent = this;//(Form1)
   frmTest .Show();

i think this code is right
checkout u r this(Form1) is mdi form
0
 

Author Comment

by:ianinspain
ID: 16958465
Hi .. no its not an mdi form... Its just a normal...

I also tried

Test frmTest = new Test();
  frmTest.Owner = this;
  frmTest.Show();

with startpositon set to Centerparent but it doesn't center..

Thanks

Ian
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16959904
As far as I know, the CenterParent option only works for MdiChildren forms.

You can position it manually...

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();          
            f2.StartPosition = FormStartPosition.Manual;
            f2.Left = (this.Left + this.Width / 2) - (f2.Width / 2);
            f2.Top = (this.Top + this.Height / 2) - (f2.Height /2);
            f2.Show();
        }
0
 

Author Comment

by:ianinspain
ID: 16967275
yes that worked great... thanks :-)
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question