Checking if control moved
-
Hello, I developed a user control, that creates a little form (X) and positions that form relative to the control. The control is placed on a windows form to test it. The idea is that when the windows main form is moved, the control is also moved (anchoring), but form X should move along. So what I do is catch the LocationChanged event of the ParentForm of the control, and in that event I reposition form X. This works ok. However, now I'm in the situation that the control is placed on something that has no ParentForm. Question: how can I detect if a control's screen coordinates are changing? So the position of the control inside its parent remains the same, but the screen coordinates change because the parent is moved. Can I detect such movement?
-
Hello, I developed a user control, that creates a little form (X) and positions that form relative to the control. The control is placed on a windows form to test it. The idea is that when the windows main form is moved, the control is also moved (anchoring), but form X should move along. So what I do is catch the LocationChanged event of the ParentForm of the control, and in that event I reposition form X. This works ok. However, now I'm in the situation that the control is placed on something that has no ParentForm. Question: how can I detect if a control's screen coordinates are changing? So the position of the control inside its parent remains the same, but the screen coordinates change because the parent is moved. Can I detect such movement?
If the control is placed in a container besides a form, then recurse up through the containers to get to the form, like so:
private Form GetContainerForm(Control c)
{
if (c == null) return null;
if (c is Form) return (Form)c;
else return GetContainerForm(c.Parent);
}Microsoft MVP, Visual C# My Articles