當(dāng)前位置:首頁(yè) > IT技術(shù) > Windows編程 > 正文

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

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

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());
        }
    }
}

輸出:

按照事件的寫法,這樣子可能更標(biāo)準(zhǔn)一點(diǎn)。

FrmA.cs 發(fā)布者

using System;
using System.Windows.Forms;

namespace SelfLianXiDelegate
{
    public delegate void AddCounter(string str);  // [1]聲明委托

    public partial class FrmA : Form
    {

        public AddCounter addCounter; // [2]創(chuàng)建委托
        //public event Action<string>  addCounter; // [2]創(chuàng)建事件
        public FrmA()
        {
            InitializeComponent();
        }

        int count = 0;
        private void btnCounter_Click(object sender, EventArgs e)
        {
            count++;
            addCounter?.Invoke(count.ToString()); // [3]發(fā)布事件
        }
    }
}

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);
        }
    }
}

輸出:

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

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

開通會(huì)員,享受整站包年服務(wù)立即開通 >