Reputation:
I've got 2 forms: A Parent and a supporting Child form.
When the Child form is initially displayed, I set something called my "Zero Point", which is where the Child form should be positioned relative to the Parent, and defined by one of the controls on the Parent:
private void miShowChild_Click(object sender, EventArgs e) {
if ((m_childForm == null) || m_childForm .IsDisposed) {
m_formLeft = this.Left + this.Control1.Left;
m_formTop = this.Top + this.Control1.Top;
m_childForm = new ChildForm1();
m_childForm.Show(this);
m_childForm.Parent_Shift(m_formTop, m_formLeft);
}
m_roleMap.TopLevel = true;
}
When the Parent form is moved somewhere else, that information is translated to the Child form:
private void Form1_MoveResize(object sender, EventArgs e) {
if ((m_childForm != null) && !m_childForm.IsDisposed) {
m_formLeft = this.Left + this.Control1.Left;
m_formTop = this.Top + this.Control1.Top;
m_childForm.Parent_Shift(m_formTop, m_formLeft);
}
}
That logic seems OK, right?
So, what happens in the Child form?
First, I've got these variables defined:
private bool m_automate;
private int m_left, m_top;
private Point m_zero;
private void ChildForm_Load(object sender, EventArgs e) {
m_left = 0;
m_top = 0;
m_zero = Point.Empty;
}
When the Child is set using Parent_Shift
, I want to get where this "Zero Point" should be defined:
public void Parent_Shift(int parentTop, int parentLeft) {
m_automate = true;
if (m_zero != Point.Empty) {
this.Left = parentLeft - m_left;
this.Top = parentTop - m_top;
} else {
this.Left = parentLeft;
this.Top = parentTop;
}
m_zero = new Point(this.Left, this.Top);
m_automate = false;
}
When the Child form is repositioned, and not because of the Parent moving, then I want to record how far from my "Zero Point" this is:
private void Form_MoveResize(object sender, EventArgs e) {
if (!m_automate && this.Visible) {
if (m_zero != Point.Empty) {
m_left = m_zero.X - this.Left;
m_top = m_zero.Y - this.Top;
} else {
m_zero = new Point(this.Left, this.Top);
}
}
}
All of the methods are firing, but I can't seem to get the coordinate system figured out.
I can move the Child form around manually, then move the Parent form and the Child form warps to some other location - then moves proportional to the Parent.
I want the Child to remain at some (X, Y) coordinates relative to the Parent.
When the Child form is moved, that relative coordinate needs to be updated to be where ever the Child form was left.
Upvotes: 0
Views: 3517
Reputation: 3255
Put this code in your parent. You don't need any code in your child form.
public partial class ParentForm : Form
{
private bool updatingChildPosition = false;
private Point childFormOffset = Point.Empty;
private ChildForm child = null;
public ParentForm()
{
InitializeComponent();
this.child = new ChildForm();
child.Show();
child.Move += child_Move;
UpdateChildFormOffset();
}
void child_Move(object sender, EventArgs e)
{
// Child form is moved, store it's new offset
UpdateChildFormOffset();
}
private void UpdateChildFormOffset()
{
if (!updatingChildPosition)
{
this.childFormOffset = new Point(
this.child.Location.X - this.Location.X,
this.child.Location.Y - this.Location.Y);
}
}
private void ParentForm_Move(object sender, EventArgs e)
{
// Updating child position
this.updatingChildPosition = true;
child.Location = new Point(
this.Location.X + childFormOffset.X,
this.Location.Y + childFormOffset.Y);
this.updatingChildPosition = false;
}
}
Upvotes: 2