C#委托使用

2021-06-07 23:07

阅读:678

标签:fun   mit   class   gen   box   forms   thread   art   sap   

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 void DelegateParam(List _string);

    public partial class Form2 : Form
    {
        public event DelegateParam Transmit;
        public Action action;
        public Func func;
        public Predicate predicate;
        public Form2()
        {
            InitializeComponent();
            this.button2.Click += Button2_Click;         
        }
        private void Button2_Click(object sender, EventArgs e)
        {
            Transmit?.Invoke(new List() { this.label1.Text.ToString() });
            action?.Invoke(this.label1.Text.ToString());
            int? a = func?.Invoke(this.label1.Text.ToString());
            MessageBox.Show(a.ToString());
            bool? status= predicate?.Invoke(this.label1.Text.ToString());
            MessageBox.Show(status.ToString());
        }
    }
}
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 button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Transmit += new DelegateParam(DelegateReceiveParam);
            form2.action += new Action(ActionReceiveParam);
            form2.func += new Func(FuncReceiveParam);
            form2.predicate += new Predicate(PredicateReceiveParam);
            form2.Show();
        }
        public void DelegateReceiveParam(List vs)
        {
            this.label1.Text = vs[0];
        }
        public void ActionReceiveParam(string vs)
        {
            this.label1.Text = vs;
        }
        public int FuncReceiveParam(string vs)
        {
            this.label1.Text = vs;
            return 1;
        }
        public bool PredicateReceiveParam(string vs)
        {
            this.label1.Text = vs;
            return true;
        }
    }
}

 

C#委托使用

标签:fun   mit   class   gen   box   forms   thread   art   sap   

原文地址:https://www.cnblogs.com/772933011qq/p/10717534.html


评论


亲,登录后才可以留言!