开发者

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 addin开发者_C百科g 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);
        }
    }
}


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;
        }
    }
}


Check out

Application.OpenForms

That should give you access to what you want.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜