How do I access the parent panel?

C# Trying to access the location of a panel's parent

  • I have a main form with a tab control on it. The tabs get populated by adding panels from other forms within the solution. One of these panels has some code that will pop up an options window. I want that window to align itself to the top right hand side of the main form. To do this I need the location and size of the main form. However, I cannot seem to access any property that will tell one of the panels what the location of that main form is. I've tried things like this.Parent, this.ParentForm and this.GetContainerControl(). They all return null. Any ideas? Addendum //Code for the main form: namespace WinAlignTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); tabControl1.TabPages[0].Controls.Add(new SomeApplication().panel1); } } } //Code that shows the option window namespace WinAlignTest { public partial class SomeApplication : Form { private ApplicationOptions Options; public SomeApplication() { InitializeComponent(); Options = new ApplicationOptions(); } private void button1_Click(object sender, EventArgs e) { Options.Show(); //This will always move the location to {0,0} Options.Location = new Point(base.Location.X,base.Location.Y); } } }

  • Answer:

    I'm confused, you seem to be adding a panel which belongs to SomeApplication to Form1. I would suggest you actually make SomeApplication a UserControl instead of a form: //Code for the main form: namespace WinAlignTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); tabControl1.TabPages[0].Controls.Add(new SomeApplication()); } } } //Code that shows the option window namespace WinAlignTest { public partial class SomeApplication : UserControl { private ApplicationOptions Options; public SomeApplication() { InitializeComponent(); Options = new ApplicationOptions(); } private void button1_Click(object sender, EventArgs e) { Options.Show(); // You might need to use PointToScreen here Options.Location = this.Location; } } }

codeNoob at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

the base identifier accesses a parent element. Two possible problems: First, your constructors don't explicitly extend your base constructor. it would look like this: public Form1():base(){} I still recommend making getter methods in the Form1 class. It would look something like this: public int Form1Location { get{return /*FormLocation*/} } and call it from WinAlignTest Let me know if this works.

Alan

Check out Application.OpenForms That should give you access to what you want.

ScottTx

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.