0

尝试创建代码,允许打开或新表单,例如一些带有信息的附加面板。我要求新表单正好在主表单下方打开,因此需要找到表单的一些坐标,但只能得到 0 和 0:

代码:

public int getCoordinateX()
    {
        return this.Location.X;
    }
public int getCoordinateY()
    {
        return this.Location.Y;
    }

和 on_load 下一个表单使用

private void PlayListForm_Load(object sender, EventArgs e)
    {
        mainForm formObject = new mainForm();
        this.Location = new Point(formObject.getCoordinateX(), formObject.getCoordinateY());
    }

我哪里错了?

4

2 回答 2

1

在您打开 PlayListForm 的 formMain 中执行以下操作:(这就是您应该打开新表单的方式)

        PlayListForm newForm = new PlayListForm(this.Location);
        newForm.Show();

现在在 PlayListForm 表单中,您需要设置它的构造函数来接收此位置,如下所示:

    public PlayListForm(Point location)
    {
        InitializeComponent();

        this.Location = location;
    }
于 2013-09-09T19:10:12.013 回答
0

您的代码的问题是您正在创建mainForm.

我刚用了这个,它奏效了。

    private void Form2_Load(object sender, EventArgs e)
    {
        mainForm form = (mainForm)Application.OpenForms["mainForm"];
        this.Location = new Point(form.GetCoordinateX(), form.GetCoordinateY());
    }
于 2013-09-09T20:16:47.213 回答