在C#中生成的另一个表单旁边显示另一个表单

Mik*_*ike 8 .net c# winforms

它是如何将有可能产生一个新的形式,例如Form2Form1,但要确保Form2毗邻Form1,例如:

在此输入图像描述

Tud*_*dor 6

就像是:

// 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坐标保持不变.


Lar*_*ech 4

尝试处理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)