Moving and placing a child window near a parent C # window

Grab everyone.

I have a form, by clicking a button, a child form is created. I want it to be created near the borders of the parent,

if the parent form is moved by the user, as well as the child window.

Thank.

+1
source share
2 answers

First, you need to save the link to the child form in the parent. Secondly, you need to pass the delegate to the parent Move and Resize events (the same delegate method is enough) Thirdly, you need to use this method to place your child form where you want.

The following code is an example of what you want:

public partial class Form1 : Form

{

  Form2 _form2;
  int _offset = 5;

  public Form1()
  {
     InitializeComponent();
     this.Move += new EventHandler(MoveSubForm);
     this.Resize +=new EventHandler(MoveSubForm);

  }

  private void Form1_Load(object sender, EventArgs e)
  {
     _form2 = new Form2();
     _form2.Show();
     MoveSubForm(this, e);
  }

  protected void MoveSubForm(object sender, EventArgs e)
  {
     if (_form2 != null)
     {
        _form2.Height = this.Height / 2;
        _form2.Width = this.Width / 3;
        _form2.Left = this.Left + this.Width + _offset;
        _form2.Top = this.Top;
     }
  }

, 5- , 1/2 1/3 , . , , .

Cheers,

+3

, , "" , "" , . , ; , ? , , LocationChanged , X Y, .

0

All Articles