two Forms move together?



  • Hi guys,

    i have two forms, one is mainForm, another is childForm.I wanna this effect that if i draw either of this two forms the other one will be followed, that means the two always move together. I wrote the code in mainForm like this

    childFrom childFm = new childFrom();
    private void mainForm_LocationChanged(object sender, EventArgs e)
            {
                Point childformLocation = new Point();
                childformLocation.X = this.Location.X ;
                childformLocation.Y = this.Location.Y + this.Height;
                childFm.Location = subformLocation;
            }
    

    then i got the result that when if move the mainForm, the subForm will follow. Now my problem is i do not know how to write the corresponding code in the childForm to make the mainForm follow when i drag the childForm.

    Anyone can bring me out



  • If the sender parameter of in event handler is the moved form, than you could solve it like that:

    Create an event handler that handles both of those cases:
    if sender is the main form => move the child.
    if it's the child => move the main form.

    Just register this handler for the LocationChanged event of both forms.

    Cheers



  • bja schrieb:

    If the sender parameter of in event handler is the moved form, than you could solve it like that:

    Create an event handler that handles both of those cases:
    if sender is the main form => move the child.
    if it's the child => move the main form.

    Just register this handler for the LocationChanged event of both forms.

    Cheers

    hi, thanks for your quick reply. I am new to C#, so could u give me same sample code?

    Thx



  • hi,

    anyone could explain how can i implement this effect in detail.
    i am urgent.

    Thx in advance!!



  • ....

    Swap main and child.

    private void childForm_LocationChanged(object sender, EventArgs e)
            {
                Point mainformLocation = new Point();
                mainformLocation.X = this.Location.X ;
                mainformLocation.Y = this.Location.Y - this.Height;
                mainFm.Location = subformLocation;
            }
    


  • Knuddlbaer schrieb:

    ....

    Swap main and child.

    private void childForm_LocationChanged(object sender, EventArgs e)
            {
                Point mainformLocation = new Point();
                mainformLocation.X = this.Location.X ;
                mainformLocation.Y = this.Location.Y - this.Height;
                mainFm.Location = subformLocation;
            }
    

    it seems not so easy. Because i can not register mainForm in childForm.cs like this.

    mainForm mainFm = new mainForm();

    if i do so, the mainForm will not show up. i do not know why?

    So how can i access the instance of the mainForm from childForm code.



  • Pls view the Properties from your form in MSDN. You will find Parent, MdiParent, Owner etc


Anmelden zum Antworten