• 當前位置:首頁 > IT技術 > Windows編程 > 正文

    C# WInform 通過委托跨窗體傳值
    2021-09-24 09:16:32

    不同窗體處于不同線程,相互之間需要通信時,需要用到委托事件。

    Form1.cs:

    using System.Windows.Forms;
    
    namespace SelfLianXiDelegate
    {
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public void ChangeLblText(string str)
            {
                this.lblCounter.Text = str;
            }
        }
    }
    

    Form2.cs:

    using System;
    using System.Windows.Forms;
    
    namespace SelfLianXiDelegate
    {
        //public delegate void AddCounter(string str);
    
        public partial class Form2 : Form
        {
    
            //public AddCounter addCounter;
            Action<string> addCounter;
            public Form2()
            {
                InitializeComponent();
                Form1 form1 = new Form1();
                addCounter = form1.ChangeLblText;
                form1.Show();
            }
    
            int count = 0;
            private void btnCounter_Click(object sender, EventArgs e)
            {
                count++;
                if (addCounter != null) {
                    addCounter(count.ToString());
                }
            }
        }
    }
    
    

    Program.cs:

    using System.Windows.Forms;
    
    namespace SelfLianXiDelegate
    {
        class Program
        {
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form2());
            }
        }
    }
    

    輸出:

    按照事件的寫法,這樣子可能更標準一點。

    FrmA.cs 發布者

    using System;
    using System.Windows.Forms;
    
    namespace SelfLianXiDelegate
    {
        public delegate void AddCounter(string str);  // [1]聲明委托
    
        public partial class FrmA : Form
        {
    
            public AddCounter addCounter; // [2]創建委托
            //public event Action<string>  addCounter; // [2]創建事件
            public FrmA()
            {
                InitializeComponent();
            }
    
            int count = 0;
            private void btnCounter_Click(object sender, EventArgs e)
            {
                count++;
                addCounter?.Invoke(count.ToString()); // [3]發布事件
            }
        }
    }
    

    FrmB.cs 訂閱者

    using System.Windows.Forms;
    
    namespace SelfLianXiDelegate
    {
    
        public partial class FrmB : Form
        {
            public FrmB(FrmA frmA)
            {
                InitializeComponent();
                frmA.addCounter = ChangeLblText; // [4]訂閱事件
               // frmA.addCounter += ChangeLblText; // [4]訂閱事件
            }
    
            public void ChangeLblText(string str) // [5]事件處理程序
            {
                this.lblCounter.Text = str;
            }
        }
    }
    
    

    主程序

    using System.Windows.Forms;
    
    namespace SelfLianXiDelegate
    {
        class Program
        {
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                FrmA frmA = new FrmA();
                FrmB frmB = new FrmB(frmA);
                frmB.Show();
    
                Application.Run(frmA);
            }
        }
    }
    
    

    輸出:

    事件本質上就是委托的運用。但是直接用委托有種情況不安全,在訂閱者中可以讓委托 = null,全部失效。事件就不一樣,只允許+=-+,除非自己的內部可以使用=。

    本文摘自 :https://www.cnblogs.com/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码