Controls define their position in relation to the owning window such as the
parent form. When a form is moved, the underlying ScrollWindow API is used
to shift the pixel content of the form to another place and the controls
remain fixed with respect to the parent window, therefore they dont know or
care whether they have moved.
To detect such a change you need to register a handler with the
LocationChanged event of the parent form. If the control is nested deeper in
the form you must walk the list of parents to find the one which has no
parent. This should be the main window.
See the code below my signature.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
----------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DetectLocationChanged
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private childControl groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.GroupBox groupBox2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new DetectLocationChanged.childControl();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(16, 32);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(300, 50);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Child control";
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(288, 23);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 0);
this.label2.Name = "label2";
this.label2.TabIndex = 0;
//
// label3
//
this.label3.Location = new System.Drawing.Point(0, 0);
this.label3.Name = "label3";
this.label3.TabIndex = 0;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.groupBox1);
this.groupBox2.Location = new System.Drawing.Point(8, 48);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(272, 96);
this.groupBox2.TabIndex = 3;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "intermediate control";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_LocationChanged(object sender, System.EventArgs e)
{
this.label1.Text=string.Format("My location
is:{0},{1}",this.Location.X,this.Location.Y);
}
private void groupBox1_LocationChanged(object sender, System.EventArgs e)
{
this.label2.Text=string.Format("My location
is:{0},{1}",this.groupBox1.Location.X,this.groupBox1.Location.Y);
}
}
class childControl : GroupBox
{
protected override void OnCreateControl()
{
base.OnCreateControl ();
Control topControl=this.Parent;
while(topControl.Parent!=null)
topControl=topControl.Parent;
topControl.LocationChanged+=new EventHandler(Parent_LocationChanged);
}
private void Parent_LocationChanged(object sender, EventArgs e)
{
Control topControl=this.Parent;
while(topControl.Parent!=null)
topControl=topControl.Parent;
this.Text=string.Format("Form location:{0},{1} My
location:{2},{3}",topControl.Location.X,topControl.Location.Y,this.Location.X,this.Location.Y);
}
}
}