就像是:
// button click handler method
Form2 child = new Form2();
child.Location = new Point(this.Location.X + this.Width,
this.location.Y);
child.Show();
Run Code Online (Sandbox Code Playgroud)
获取当前表单对象位置的X坐标,并将表单的宽度添加到其中,从而获得新表单的X坐标.Y坐标保持不变.
尝试处理LocationChanged主窗体的事件。
简单演示:
public partial class Form1 : Form {
Form2 f2;
public Form1() {
InitializeComponent();
this.LocationChanged += new EventHandler(Form1_LocationChanged);
}
private void button1_Click(object sender, EventArgs e) {
f2 = new Form2();
f2.StartPosition = FormStartPosition.Manual;
f2.Location = new Point(this.Right, this.Top);
f2.Height = this.Height;
f2.Show();
}
void Form1_LocationChanged(object sender, EventArgs e) {
if (f2 != null)
f2.Location = new Point(this.Right, this.Top);
}
}
Run Code Online (Sandbox Code Playgroud)