C# Window Data Transfer Guide

In C#, you can achieve data transfer between windows by using delegates.

First, define a delegate type and event in the form for sending data to pass the data.

public delegate void DataTransferDelegate(string data);
public event DataTransferDelegate DataTransferEvent;

Next, subscribe to the event in the data-receiving form and define the corresponding handling method.

public void SubscribeDataTransferEvent()
{
    // 订阅事件
    senderForm.DataTransferEvent += new DataTransferDelegate(ReceiveData);
}

public void ReceiveData(string data)
{
    // 处理接收到的数据
}

Finally, trigger the event in the form where the data is being sent and pass the data to the subscriber.

if(DataTransferEvent != null)
{
    DataTransferEvent("Hello, World!");
}

This allows for the functionality of transferring data between different windows.

bannerAds