代理實現兩個視窗之間的通訊

來源:互聯網
上載者:User
namespace DelegateTest
{
    public partial class Form1 : Form
    {
        public delegate void ShowTextValue(string text);//代理
        public event ShowTextValue showText;//代理事件
        public Form1()
        {
            InitializeComponent();
            //把事件加入事件隊列中
            showText += new ShowTextValue(SetText);
        }
         
        //開始代理
        public void StartDelegate(string str) {
            showText(str);
        }

        //設定文字框的值
        private void SetText(string str) {
            textBox1.Text = str;
        }

        //textBox1的TextChange事件
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            showText(textBox1.Text);
        }

        
       
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //在關閉視窗時 去掉代理事件,因為沒載入一次表單就代理了SetText方法,
            //如果不去掉,這個視窗開幾次 ,SetText方法就會執行幾次
            //事件隊列中的方法會按順序執行
            showText -= new ShowTextValue(SetText);
        }
    }
}
 
namespace DelegateTest
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2()
        {
            InitializeComponent();          
        }

        private void SetText(string text)
        {
            textBox1.Text = text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form1 = new Form1();
            form1.showText += new Form1.ShowTextValue(SetText); 
            form1.Show();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            form1.StartDelegate(textBox1.Text);
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            form1.showText -= new Form1.ShowTextValue(SetText);
        }        
    }
}
namespace DelegateTest
{
  public  class Control
    {
        public delegate void SomeHandler(object sender, EventArgs e);
        public event SomeHandler someevent;

        public Control() {
            this.someevent += new SomeHandler(PrintStr);
            this.someevent += new SomeHandler(PrintInt);
        }
        public void ReadStr() {
            EventArgs e = new EventArgs();
            someevent(this,e);            
        }

        private void PrintStr(object sender, EventArgs e)
        {             
            MessageBox.Show("我就是陳太漢!,陳曉玲就是我老婆","代理");           
        }

        private void PrintInt(object sender, EventArgs e)
        {
            MessageBox.Show("你好,我就是陳太漢!,陳太漢就是我,哈哈哈哈哈", "代理");          
      }
    }
}
namespace DelegateTest
{
   public class Container
    {
       Control control = new Control();

       public Container() {
           control.someevent += new Control.SomeHandler(DelegateC);
           control.ReadStr();
       }

       private void DelegateC(object sender, EventArgs e)
       {
            
           MessageBox.Show("代理陳曉玲", "代理");
       }
    }
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.