• 步步为营-27-事件


    1:先用委托演示

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Event_PlayMusic
    {
        //声明委托
        public delegate string delPlay();
        class PlayMusic
        {
            //定义委托
            public delPlay del;
            private string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public PlayMusic(string name)
            {
                this.name = name;
            }
    
            public void PlaySongs()
            {
                Thread.Sleep(3000);
                if (del != null)
                {
                    del();
                }
            }
    
        }
    }
    Form
    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 Event_PlayMusic
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnPlaySong_Click(object sender, EventArgs e)
            {
                string songName = "小天狼";
                //事件--先触发后响应
                PlayMusic p = new PlayMusic(songName);
                MessageBox.Show("开始播放" + songName);
                p.del = () => { return "播放完毕"; };
                p.PlaySongs();
                MessageBox.Show(p.del());
             
            }
        }
    }
    Form

    效果
    将委托改为事件

    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 Event_PlayMusic
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnPlaySong_Click(object sender, EventArgs e)
            {
                string songName = "小天狼";
                //事件--先触发后响应
                PlayMusic p = new PlayMusic(songName);
                MessageBox.Show("开始播放" + songName);
                p.del += () => { return "播放完毕"; };            
               string str = p.PlaySongs();
               MessageBox.Show(str);
             
            }
        }
    }
    Form1
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Event_PlayMusic
    {
        //声明委托
        public delegate string delPlay();
        class PlayMusic
        {
            //定义委托
            public event delPlay del;
            private string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            public PlayMusic(string name)
            {
                this.name = name;
            }
    
            public string PlaySongs()
            {
                Thread.Sleep(3000);
                if (del != null)
                {
                   return  del();
                }
    
                return "事件为空!";
            }
    
        }
    }
    PlayMusic
  • 相关阅读:
    洛谷—— P1187 3D模型
    商铺项目(Redis缓存)
    商铺项目(使用DES加密配置信息)
    Linux下tomcat启动项目原因排查
    商铺项目(商品详情页开发)
    商铺项目(店铺详情页开发)
    商铺项目(店铺列表开发)
    商铺项目(首页前端开发)
    商铺项目(首页后台开发)
    商铺项目(商品类别模块二)
  • 原文地址:https://www.cnblogs.com/YK2012/p/6738723.html
Copyright © 2020-2023  润新知