Solved

Centering a form using parent?

Posted on 2006-06-19
8
467 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

695 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