?
Solved

Centering a form using parent?

Posted on 2006-06-19
8
Medium Priority
?
469 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
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

777 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