One thing you can't do in "regular" WinForms (without getting into low-level Window Events and using Platform Invoike) is to have constant (incremental) tracking of one Form moving as another moves by click-dragging on the TitleBar: this has to do with the innate behavior of the TitleBar area.
The example shown here handles the case that the Form you wish tracked may change ... and overlap the Form that follows it ... in either of two ways:
1. As the Location of the Form is changed by click-dragging the TitleBar of the Form ... or, in code.
2. As the Size of the Form is changed by direct-action ... or, in code.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace YourNameSpace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 f2 = new Form2();
private void Form1_Load(object sender, EventArgs e)
{
f2.Show();
f2.LocationChanged += Form1_SizeAndLocationChanged;
f2.SizeChanged += Form1_SizeAndLocationChanged;
Form1_SizeAndLocationChanged(sender, e);
}
private void PositionForm2(int fLeft, int fTop)
{
f2.Location = new Point(fLeft, fTop);
}
private void Form1_SizeAndLocationChanged(object sender, EventArgs e)
{
PositionForm2(this.Right, this.Top);
}
}
}
Discussion:
1. This code requires you assign at design-time the EventHandlers for both Form1 LocationChanged and SizeChanged Events to the Form1_SizeAndLocationChanged method.
2. There are some issues with the simple example shown here that should be addressed for production quality code:
a. What if the goal is to
never have any part of Form2 moved outside the Screen boundaries ?
b. What should happen if Form1 is Minimized, or Maximized ?