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
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
Related Q & A:
- How to access the value of a field of the current node in Drupal?Best solution by drupal.org
- What is a receptionist's job in a doctor's office?Best solution by wisegeek.org
- Do I have to have a bachelor's degree before going for a master's degree in the U.S?Best solution by Yahoo! Answers
- For a U.S. Passport, what's the difference exactly between a passport book and a passport card?Best solution by ChaCha
- Can Someone Find Your Exact Location From a Website?Best solution by foxnews.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.