问题描述
|
我有一个带有选项卡控件的主窗体。通过在解决方案中添加其他表单的面板来填充选项卡。这些面板之一具有一些代码,这些代码将弹出一个选项窗口。我希望该窗口与主窗体的右上角对齐。为此,我需要主窗体的位置和大小。但是,我似乎无法访问任何将告诉面板之一的主窗体的位置属性。
我已经尝试过诸如
this.Parent
,this.ParentForm
和ѭ2things之类的东西。他们都返回null
。
有任何想法吗?
附录
//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);
}
}
}
解决方法
我很困惑,您似乎正在向Form1添加一个属于SomeApplication的面板。我建议您实际上使SomeApplication成为UserControl而不是表单:
//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;
}
}
}
, 查看
Application.OpenForms
那应该使您能够访问所需的内容。
, 基本标识符访问父元素。
两个可能的问题:首先,构造函数没有显式扩展基本构造函数。它看起来像这样:
public Form1():base(){}
我仍然建议在Form1类中使用getter方法。它看起来像这样:
public int Form1Location
{
get{return /*FormLocation*/}
}
并从WinAlignTest调用它
让我知道这个是否奏效。