Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
When I run my program, two forms comes up.
Form1 and Form2.
Now what I need is when I move the placement of form1 to anywhere of screen via mouse, form2 moves where ever form1 goes.
I mean I need Form2 to be lock on Form1.
How????

Thanks.
Posted

 
Share this answer
 
Comments
Sh.H. 9-Dec-13 17:13pm    
if I use Event.LocationChanged then Form2 will change the location after I move Form1.
What I need is during moving Form1, Form2 moves where ever I move Form1, not after that.
Ok?
Sergey Alexandrovich Kryukov 9-Dec-13 18:00pm    
It solves the problem, my 5.
Not try to talk with OP... (or better look at his previous posts...) :-(
—SA
Maciej Los 10-Dec-13 12:00pm    
Thank you, Sergey ;)
I saw your comment yesterday (on mobilephone), but it was late.
I've been warned ;)
Sergey Alexandrovich Kryukov 10-Dec-13 12:04pm    
:-)
One thing you can't do in "regular" WinForms (without getting into low-level Window Events and using Platform Invoike) is to have constant (incremental) tracking of one Form moving as another moves by click-dragging on the TitleBar: this has to do with the innate behavior of the TitleBar area.

The example shown here handles the case that the Form you wish tracked may change ... and overlap the Form that follows it ... in either of two ways:

1. As the Location of the Form is changed by click-dragging the TitleBar of the Form ... or, in code.

2. As the Size of the Form is changed by direct-action ... or, in code.

C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Form2 f2 = new Form2();

        private void Form1_Load(object sender, EventArgs e)
        {
            f2.Show();

            // the Form2 instance will use the same EventHandlers
            // that the Form1 instance (Main Form) uses
            f2.LocationChanged += Form1_SizeAndLocationChanged;
            f2.SizeChanged += Form1_SizeAndLocationChanged;

            // need to adjust the position on Application start
            // note: you could just as well call this with
            // both arguments being 'null
            Form1_SizeAndLocationChanged(sender, e);
        }

        // convenience method
        private void PositionForm2(int fLeft, int fTop)
        {
            f2.Location = new Point(fLeft, fTop);
        }

        // Both the Form1 instance LocationChanged,
        // and SizeChanged Events use this EventHandler
        private void Form1_SizeAndLocationChanged(object sender, EventArgs e)
        {
            // experiment using one of these method calls

            // Form2 left, top docked Form1 right, top
            PositionForm2(this.Right, this.Top);

            // Form2 right, top docked Form1 left, top
            //PositionForm2(this.Left - f2.Width, this.Top);

            // Form2 left, top docked Form1 right, Bottom
            // PositionForm2(this.Right, this.Bottom);

            // Form2 right, top docked Form1 left, bottom
            //PositionForm2(this.Left - f2.Width, this.Bottom);
        }

        // try experimenting with this with the code in
        // Form1_SizeAndLocationChanged commented out
        // and observe the differences 
        //private void Form1_MouseMove(object sender, MouseEventArgs e)
        //{
            //Form1_SizeAndLocationChanged(sender, e);
        //}
    }
}
Discussion:

1. This code requires you assign at design-time the EventHandlers for both Form1 LocationChanged and SizeChanged Events to the Form1_SizeAndLocationChanged method.

2. There are some issues with the simple example shown here that should be addressed for production quality code:

a. What if the goal is to never have any part of Form2 moved outside the Screen boundaries ?

b. What should happen if Form1 is Minimized, or Maximized ?
 
Share this answer
 
By Delegate.I think the way through the commission to move when one form, modify the position of another form
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900