Solved

Centering a form using parent?

Posted on 2006-06-19
8
463 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
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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 85

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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:…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now