473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

OnMove for a UserControl

How can I get an OnMove message for a UserControl?
Mar 14 '07 #1
5 1921
Seems like it inherits from Control which has a OnMove handler, but I never
get it.

"Joachim" wrote:
How can I get an OnMove message for a UserControl?
Mar 14 '07 #2
Works fine for me...

Note that this is relative to the container, so if the container is
moved you won't get it... you'd need to attach events all the way up
to the form to catch this.
using System;
using System.Windows.Forms;
using System.Drawing;

class Test : UserControl {
static void Main() {
Random rand = new Random();
using(Form f = new Form())
using(Button b = new Button())
using (Test t = new Test()) {
b.Dock = DockStyle.Top;
f.Controls.Add(b);
f.Controls.Add(t);
b.Text = "Move";
b.Click += delegate {
t.Location = new Point(rand.Next(f.Width),
rand.Next(f.Height));
};
Application.Run(f);
}
}

protected override void OnMove(EventArgs e) {
ParentForm.Text = "OnMove:" + Location;
}
}
Mar 14 '07 #3
To complete this to detect a change at any level:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;

class Test : UserControl {
static void Main() {
Random rand = new Random();
using(Form f = new Form())
using(Button b = new Button())
using (Test t = new Test()) {
b.Dock = DockStyle.Top;
f.Controls.Add(b);
f.Controls.Add(t);
b.Text = "Move";
b.Click += delegate {
t.Location = new Point(rand.Next(f.Width),
rand.Next(f.Height));
};
Application.Run(f);
}
}
private readonly List<Controlhierarchy = new List<Control>();
private void Bind(bool hook) {
foreach(Control c in hierarchy) { // detach first
c.Move -= c_Move;
c.ParentChanged -= c_ParentChanged;
}
hierarchy.Clear();
if (hook) { // re-attach if desired
Control c = Parent; // could equally start with self and
drop 2xOn[blah]
while (c != null) {
c.Move += c_Move;
c.ParentChanged += c_ParentChanged;
hierarchy.Add(c);
c = c.Parent;
}
YeehahMoved();
}

}
protected override void Dispose(bool disposing) {
if (disposing) {
Bind(false);
}
base.Dispose(disposing);
}
void c_ParentChanged(object sender, EventArgs e) {
Bind(true); // hierarchy has changed...
}

void c_Move(object sender, EventArgs e) {
YeehahMoved(); // somebody moved and it wasn't us!
}

private void YeehahMoved() {
if(ParentForm!=null) ParentForm.Text =
PointToScreen(Location).ToString();
}

public Test() { // make it visible
BorderStyle = BorderStyle.FixedSingle;
}

protected override void OnParentChanged(EventArgs e) {
base.OnParentChanged(e);
Bind(true);
}

protected override void OnMove(EventArgs e) {
base.OnMove(e);
YeehahMoved();
}
}
Mar 14 '07 #4
Thanks Marc,

Could perhaps have something to do with that I'm using the Composite UI
Application Block... do you happen to know something about it?

Regards,
JOachim

"Marc Gravell" wrote:
To complete this to detect a change at any level:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;

class Test : UserControl {
static void Main() {
Random rand = new Random();
using(Form f = new Form())
using(Button b = new Button())
using (Test t = new Test()) {
b.Dock = DockStyle.Top;
f.Controls.Add(b);
f.Controls.Add(t);
b.Text = "Move";
b.Click += delegate {
t.Location = new Point(rand.Next(f.Width),
rand.Next(f.Height));
};
Application.Run(f);
}
}
private readonly List<Controlhierarchy = new List<Control>();
private void Bind(bool hook) {
foreach(Control c in hierarchy) { // detach first
c.Move -= c_Move;
c.ParentChanged -= c_ParentChanged;
}
hierarchy.Clear();
if (hook) { // re-attach if desired
Control c = Parent; // could equally start with self and
drop 2xOn[blah]
while (c != null) {
c.Move += c_Move;
c.ParentChanged += c_ParentChanged;
hierarchy.Add(c);
c = c.Parent;
}
YeehahMoved();
}

}
protected override void Dispose(bool disposing) {
if (disposing) {
Bind(false);
}
base.Dispose(disposing);
}
void c_ParentChanged(object sender, EventArgs e) {
Bind(true); // hierarchy has changed...
}

void c_Move(object sender, EventArgs e) {
YeehahMoved(); // somebody moved and it wasn't us!
}

private void YeehahMoved() {
if(ParentForm!=null) ParentForm.Text =
PointToScreen(Location).ToString();
}

public Test() { // make it visible
BorderStyle = BorderStyle.FixedSingle;
}

protected override void OnParentChanged(EventArgs e) {
base.OnParentChanged(e);
Bind(true);
}

protected override void OnMove(EventArgs e) {
base.OnMove(e);
YeehahMoved();
}
}
Mar 14 '07 #5
Not a lot really... sorry ;-(

Marc
Mar 14 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: v | last post by:
Hi, For some reason the onMove event handler doesnt seem to work. Am I doing it right? The script is in a popup window. rgds, v <html>
8
by: Raed Sawalha | last post by:
Hi, I have a strange problem with a usercontrol on a page. The usercontrol dispalyes three categories (From a database) when the user clicks a category they see all the products in a shop for...
2
by: Sascha | last post by:
Hi there, I searched carefully through the web before finally deciding to post this message, because I could not find a solution for my problem. Hopefully someone will have a hint or explanation...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
3
by: crafuse | last post by:
Hello, I have an application where I have a main window and several other windows owned by it. The owned windows are derived from a class that overrides the OnMove method so that thay...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
6
by: MeowCow | last post by:
I have created a UserControl that encapsulates a third party data grid. My goal was to create my own DataSource and DataMember properties that forward the binding to the third party grid, then use...
1
by: udieee | last post by:
Hi, Could anyone tell me what is NS equivalent for IE onMove() function. TIA, --Udie
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.