C# 事件和委托1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApp1
{
 
   
    public partial class Form1 : Form
    {
        
 
        public Form1()
        {
            InitializeComponent();
            //注册事件
            
        }
 
        private void Technology_BoilderEventLog()
        {
            MessageBox.Show("执行事件");
        }
 
 
        public string UPImage(string s)
        {
 
            return s + "数据1";
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 technology = new Form2();
            technology.UpImageDelegate = UPImage;
            technology.Show();
 
        }
    }
}
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApp1
{
    //委托
    public delegate string UpImageDelegate(string s);
 
    public delegate void BoilderLogEvent();
 
    public partial class Form2 : Form
    {
        public UpImageDelegate UpImageDelegate = null;
 
        //基于上面的委托定义事件
        public event BoilderLogEvent BoilderEventLog;
 
        public Form2()
        {
            InitializeComponent();
           
        }
 
      
 
        private void button1_Click(object sender, EventArgs e)
        {
 
            label1.Text= UpImageDelegate("节点1:");
        }
 
       
    }
///////////////////////////////////////////////////////////////////////////////////////
posted @ 2022-10-31 22:28  薛定谔的箱子  阅读(27)  评论(0)    收藏  举报