日期:2014-05-18  浏览次数:20968 次

Winform多窗体跟随移动的问题!
Winform多窗体跟随移动的问题!

C#项目有两个窗体,form1为主窗体,设计为点击form1上的按钮弹出窗体form2,

1.让form2跟随form1移动,这个我已经完成了;是用在form1窗体载入时创建form2对象的,然后在form1的move事件中设置form2的top、left属性实现的方法。

2.那么怎么让form1跟随form2移动呢??谢谢了!!

------解决方案--------------------
form2 添加 move 。再传到form1就是了。窗口之间通信而已。如果form1 是 form2 parent,那更简单
------解决方案--------------------
直接用委托事件就可以了:

首先Bind1方法为无返回类型,那就用无返回类型的delegate
1、子窗体class前定义

 public delegate void Callback(string strexchange);
// 后再子窗体中添加如下代码
public event Callback Callevent=null;

//放在btnOK_Click事件中
Callevent(textbox1.text);//子窗体的this.textbox1

2.主窗体
调用委托事件
子窗体 chil = new 子窗体();
chil.Show();
chil.Callevent += new Callback(chil_Callevent);

void chil_Callevent(object sender)
{
this.textbox1.text=sender;//主窗体的this.textbox1
}

------解决方案--------------------
C# code

1、两个预览窗口的定义,是Form
 /// <summary>
        /// 预览窗口
        /// </summary>
        private ImageFrm m_ifrm = new ImageFrm();

        /// <summary>
        /// 预览窗口
        /// </summary>
        private ImageFrm1 m_ifrm1 = new ImageFrm1();

2、在主窗口的Load,初始化他们,这两个窗口会停止主窗口的右侧
/// <summary>
        /// Load...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DiaryFrm_Load(object sender, EventArgs e)
        {            
                    

            this.m_ifrm.Location = new Point(this.Location.X + this.Width, this.Location.Y);
            this.m_ifrm.Owner = this;
            this.m_ifrm.Show();

            this.m_ifrm1.Location = new Point(this.Location.X + this.Width, this.Location.Y + this.m_ifrm.Height);
            this.m_ifrm1.Owner = this;
            this.m_ifrm1.Show();
加载两个事件
this.LocationChanged += new EventHandler(DiaryFrm_LocationChanged);
            this.SizeChanged += new EventHandler(DiaryFrm_SizeChanged);     
}

3、事件代码
/// <summary>
        /// Size Changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DiaryFrm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                
            }
            else
            if (this.WindowState == FormWindowState.Maximized)
            {
                
                this.m_ifrm.Location = new Point((this.Location.X + this.Width) - this.m_ifrm.Width - 20, this.Location.Y + SystemInformation.CaptionHeight + SystemInformation.ToolWindowCaptionHeight);

                
                this.m_ifrm1.Location = new Point((this.Location.X + this.Width) - this.m_ifrm.Width - 20, this.Location.Y + this.m_ifrm.Height + SystemInformation.CaptionHeight + SystemInformation.ToolWindowCaptionHeight);
            }
            else
            {
                
                this.m_ifrm.Location = new Point(this.Location.X + this.Width, this.Location.Y);

                
                this.m_ifrm1.Location = new Point(this.Location.X + this.Width, this.Location.Y + this.m_ifrm.Height);
            }
        }

        /// <summary>
        /// Location Changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DiaryFrm_LocationChanged(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Maximized)
            {
                this.m_ifrm.Location = new Point(this.Location.X + this.Width, this.Location.Y);
                this.m_ifrm1.Location = new Point(this.Location.X + this.