zoukankan      html  css  js  c++  java
  • C#委托机制的演示代码


    图1 带EventArgs的委托演示

    图2 不带EventArgs的委托演示

    本例源代码下载:
    1。不带参数的委托
    2。带参数的委托

    带EventArgs的委托演示例程源代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Diagnostics;

    namespace WindowsFormsApplication8
    {
        public partial class Form1 : Form
        {
            public static TextBox ttt = null;
            public Form1()
            {
                InitializeComponent();
                ttt = this.textBox1;
               
            }
                    

            p rivate void button1_Click(object sender, EventArgs e)
            {
                employee employee1 = new employee();
                employee1.Age = 33;
                employee1.Name = "猪悟能";
                admin admin1 = new admin();
                employee1.playgame +=new delegate1(admin1.notify);
                employee1.games();
               
            }
           
            p rivate class employee
            {
                String name = "";
                int age = 0;
                public event delegate1 playgame;

                public String Name
                {
                    get { return this.name; }
                    set { this.name = value; }
                }

                public int Age
                {
                    get { return this.age; }
                    set { this.age = value; }
                }

                public void games()
                {
                    if (playgame != null)
                    {
                        custom e = new custom();
                        e.Age = this.age;
                        e.Name = this.name;
                        playgame(this,e);
                    }
                }
            }

            p rivate class admin
            {
               
                public void notify(object sender,custom e)
                {
                    Form1.ttt.Text =e.Name+ 在玩游戏!年龄:"+e.Age.ToString()+"宀?;
                }
            }

            public delegate void delegate1(object sender,custom e);

            public class custom : EventArgs
            {
                String name = "";
                int age = 0;

                public String Name
                {
                    set{this.name = value;}
                    get { return this.name; }
                }

                public int Age
                {
                    set { this.age = value; }
                    get { return this.age; }
                }
            }

        }
    }

  • 相关阅读:
    MySQL 8.0.14版本新功能详解
    Uncaught TypeError: Right-hand side of 'instanceof' is not an object
    程序员快速技术提升之道
    Windows 10 cmd命令符的使用
    数据千万条,备份第一条:VFEmail被擦除所有数据面临关停
    netty-socketio 示例代码
    idea中 在接口中如何直接跳转到该接口的是实现类中?
    perl DBD处理超时问题
    springboot 启动配置文件配置
    Office Word 发布文章到博客园
  • 原文地址:https://www.cnblogs.com/hackpig/p/1668473.html
Copyright © 2011-2022 走看看