Solved

Centering a form using parent?

Posted on 2006-06-19
8
462 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

17 Experts available now in Live!

Get 1:1 Help Now