Getting Notification of Moving of Form

B

Bradley Remedios

I have a control that I need to get notification of when it's screen
position changes.

I have no problem getting the Resize event from its parent form, but
cannot get notification when the Form itself is moved by the user. I
only get notification via the events when the Form is initially
constructed.

I have tried listening on the Move and LocationChanged Events at the
level of the Control all the way up to the TopLevelControl without any
success.

I register for the event (Substitue LocationChanged for Move for the
Move event) as follows:

pControl->LocationChanged += gcnew System::EventHandler (this,
&EventHandlingClass::OnMove);

Where pControl is the control that I want to know when its screen
position changes and EventHandlingClass is the same class as the this
instance. I have also tried pControl->Parent->LocationChanged and
pControl->TopLevelControl->LocationChanged without any difference.

Any help is greatly appreciated,
Brad.
 
B

Bob Powell [MVP]

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);

}

}

}
 
B

Bradley Remedios

Thanks for the help. The problem ended up being that my parent was
being changed so after registering for the parent changed event and
de-registering / re-registering the events everything worked.

Brad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top