how to move two windows forms together?

前端 未结 2 808

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 in design. how

2条回答
  •  情歌与酒
    2020-12-20 09:51

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

    class FormConnector
    {
        private Form mMainForm;
    
        private List
    mConnectedForms = new List(); 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); } }

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

提交回复
热议问题