如何将两个窗体一起移动

如何将两个窗体一起移动

本文介绍了如何将两个窗体一起移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下 btn 时我有主表单我用 showDialog() 函数打开新表单,当我按下主窗体时,我需要将两个窗体移动到一起,因为它们共享设计.我怎样才能将它们移动到一起,要么按下主窗体并移动它,要么按下 form2 并移动它?感谢您提供任何建议.

I've main form when I press btn I open new form with showDialog() function,I need to move two forms together when I press on main form, because they share indesign.how can I move them together either I press on main form and move it or I press on form2 and move it?Thx alot for any suggestion.

推荐答案

您可以创建一个单独的类来管理表单连接和事件处理.

You could create a separate class to manage the form connections and event handling.

class FormConnector
{
    private Form mMainForm;

    private List<Form> mConnectedForms = new List<Form>();

    private Point mMainLocation;

    public FormConnector(Form mainForm)
    {
        this.mMainForm = mainForm;
        this.mMainLocation = new Point(this.mMainForm.Location.X, this.mMainForm.Location.Y);
        this.mMainForm.LocationChanged += new EventHandler(MainForm_LocationChanged);
    }

    public void ConnectForm(Form form)
    {
        if (!this.mConnectedForms.Contains(form))
        {
            this.mConnectedForms.Add(form);
        }
    }

    void MainForm_LocationChanged(object sender, EventArgs e)
    {
        Point relativeChange = new Point(this.mMainForm.Location.X - this.mMainLocation.X, this.mMainForm.Location.Y - this.mMainLocation.Y);
        foreach (Form form in this.mConnectedForms)
        {
            form.Location = new Point(form.Location.X + relativeChange.X, form.Location.Y + relativeChange.Y);
        }

        this.mMainLocation = new Point(this.mMainForm.Location.X, this.mMainForm.Location.Y);
    }
}

现在您要做的就是实例化一个 FormConnector 并使用您要连接的表单调用 ConnectForm 方法.

Now all you have to do is to instantiate a FormConnector and call ConnectForm method with the form you want to connect to.

这篇关于如何将两个窗体一起移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:20