Child form follow main form

前端 未结 2 1752
孤城傲影
孤城傲影 2020-12-21 05:44

How to make the child form follow main form.

for example: open a winform [.net2], winform opens form, form follows the mainform if mainform is moving.

相关标签:
2条回答
  • 2020-12-21 06:27

    Use the LocationChanged event from the MainForm to always set the location of the ChildForm.

    Working example:

    Form childForm = new Form();
    
    protected override void OnLoad(EventArgs e) {
      base.OnLoad(e);
    
      childForm.StartPosition = FormStartPosition.Manual;
      childForm.Width = this.Width;
      childForm.Height = 96;
      childForm.Location = new Point(this.Left, this.Bottom);
      childForm.Show();
    }
    
    protected override void OnLocationChanged(EventArgs e) {
      base.OnLocationChanged(e);
    
      if (childForm != null) {
        childForm.Location = new Point(this.Left, this.Bottom);
      }
    }
    
    0 讨论(0)
  • 2020-12-21 06:28

    Superficially simple answer is just to add handlers for when Mainform is moved or resized and then set childform Location and size accordingly.

    However do you say want to stop main form being moved such that childform end up off screen. Can child form be moved independantly. What about minimise and maximise?

    Might you want other arrangements, nore than one child, left and right, child form above main form...

    Be worth writing a layout class, and shoving all this stuff off to it.

    0 讨论(0)
提交回复
热议问题