梓骏

导航

Winform数据即时更新 父窗体子窗体即时更新

第一种方法:
用委托,Form2和Form3是同一组
Form2

C#代码 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8.  
  9. namespace TestMouseMove 
  10.     publicdelegatevoid SetVisiableHandler(); 
  11.  
  12.     public partial class Form2 : Form 
  13.     { 
  14.         public Form2() 
  15.         { 
  16.             InitializeComponent(); 
  17.         } 
  18.         privatevoid button1_Click(object sender, EventArgs e) 
  19.         { 
  20.             Form3 frm = new Form3(new SetVisiableHandler(SetVisiable)); 
  21.             frm.Show(); 
  22.         } 
  23.  
  24.         privatevoid SetVisiable() 
  25.         { 
  26.             SetVisiable(this.label1, !this.label1.Visible); 
  27.         } 
  28.  
  29.         privatevoid SetVisiable(Control control, bool visiable) 
  30.         { 
  31.             if (this.Controls.Contains(control)) 
  32.             { 
  33.                 control.Visible = visiable; 
  34.             } 
  35.         } 
  36.  
  37.     } 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestMouseMove
{
    public delegate void SetVisiableHandler();

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form3 frm = new Form3(new SetVisiableHandler(SetVisiable));
            frm.Show();
        }

        private void SetVisiable()
        {
            SetVisiable(this.label1, !this.label1.Visible);
        }

        private void SetVisiable(Control control, bool visiable)
        {
            if (this.Controls.Contains(control))
            {
                control.Visible = visiable;
            }
        }

    }
}

Form3

C#代码 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8.  
  9. namespace TestMouseMove 
  10.     public partial class Form3 : Form 
  11.     { 
  12.         private SetVisiableHandler m_setVisible; 
  13.  
  14.         public Form3(SetVisiableHandler setvisible) 
  15.         { 
  16.             InitializeComponent(); 
  17.             this.m_setVisible = setvisible; 
  18.         } 
  19.         privatevoid btnVisible_Click(object sender, EventArgs e) 
  20.         { 
  21.             if (this.m_setVisible != null
  22.             { 
  23.                 this.m_setVisible(); 
  24.             } 
  25.         } 
  26.  
  27.     } 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestMouseMove
{
    public partial class Form3 : Form
    {
        private SetVisiableHandler m_setVisible;

        public Form3(SetVisiableHandler setvisible)
        {
            InitializeComponent();
            this.m_setVisible = setvisible;
        }
        private void btnVisible_Click(object sender, EventArgs e)
        {
            if (this.m_setVisible != null)
            {
                this.m_setVisible();
            }
        }

    }
}

第二种方法:
用变量,Form4和Form5是同一组
Form4

C#代码 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8.  
  9. namespace TestMouseMove 
  10.     public partial class Form4 : Form 
  11.     { 
  12.         public Form4() 
  13.         { 
  14.             InitializeComponent(); 
  15.         } 
  16.         #region 子窗口刷新父窗口的值 
  17.  
  18.         privatestring strLabel1 = ""
  19.  
  20.         publicstring StrLabel1 
  21.         { 
  22.             get 
  23.             { 
  24.                 return strLabel1; 
  25.             } 
  26.             set 
  27.             { 
  28.                 strLabel1 = value; 
  29.                 this.label1.Text = strLabel1; 
  30.             } 
  31.         } 
  32.         #endregion 
  33.  
  34.         privatevoid button1_Click(object sender, EventArgs e) 
  35.         { 
  36.             Form5 form5 = new Form5(this);//这里注意传个this 
  37.             form5.Show(); 
  38.         } 
  39.     } 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestMouseMove
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        #region 子窗口刷新父窗口的值

        private string strLabel1 = "";

        public string StrLabel1
        {
            get
            {
                return strLabel1;
            }
            set
            {
                strLabel1 = value;
                this.label1.Text = strLabel1;
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            Form5 form5 = new Form5(this);//这里注意传个this
            form5.Show();
        }
    }
}

Form5

C#代码 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8.  
  9. namespace TestMouseMove 
  10.     public partial class Form5 : Form 
  11.     { 
  12.         Form4 form4 = new Form4(); 
  13.  
  14.         public Form5(Form4 formFrm)//这个构造方法里有参数 
  15.         { 
  16.             form4 = formFrm; //这个必须要有 
  17.             InitializeComponent(); 
  18.         } 
  19.  
  20.         
  21.         privatevoid button1_Click(object sender, EventArgs e) 
  22.         { 
  23.             form4.StrLabel1 = this.textBox1.Text; 
  24.         } 
  25.     } 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestMouseMove
{
    public partial class Form5 : Form
    {
        Form4 form4 = new Form4();

        public Form5(Form4 formFrm)//这个构造方法里有参数
        {
            form4 = formFrm; //这个必须要有
            InitializeComponent();
        }

       
        private void button1_Click(object sender, EventArgs e)
        {
            form4.StrLabel1 = this.textBox1.Text;
        }
    }
}

来自网络.....

posted on 2012-08-22 14:41  梓骏  阅读(827)  评论(0编辑  收藏  举报