博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#类对象的事件定义
阅读量:4312 次
发布时间:2019-06-06

本文共 2012 字,大约阅读时间需要 6 分钟。

1. 类对象代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Timers; namespace WinformEventTest{    ///     /// 定义了一个 ShowString 事件的对象类    ///     internal class EventClass    {        ///           /// 声明委托          ///           /// 委托传递的参数          public delegate void BroadcastEventHander(string a);        ///         /// 声明委托相关的事件        ///         public event BroadcastEventHander Broadcast;        ///         /// 声明定时器        ///         private Timer _timer;        ///         ///         ///         public EventClass()        {            _timer = new Timer(1000);            _timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);            _timer.Enabled = true;        }        ///         /// 内部定时器事件,用于模拟产生事件        ///         ///         ///         void Timer_Elapsed(object sender, ElapsedEventArgs e)        {            try            {                Broadcast("来自 EventClass 对象事件的消息:" + DateTime.Now.ToString()); // 产生事件            }            catch (Exception)            {            }        }    }}

 

2.Winform调用举例

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;namespace WinformEventTest{    public partial class Form1 : Form    {        // 声明变量        EventClass _eventClass;        ///           /// 控制台打印字符串        ///           ///           public void ConsoleShowTxt(string a)        {            Console.WriteLine(DateTime.Now.ToString() + " | " + a + "\n");        }                public Form1()        {            InitializeComponent();            // Member initialize            _eventClass = new EventClass();            // Member event initialize            _eventClass.Broadcast += new EventClass.BroadcastEventHander(ConsoleShowTxt); // 委托类事件(Broadcast)绑定实际处理方法(ConsoleShowTxt)        }    }}

 

posted on
2016-09-24 15:51 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/jayhust/p/5903403.html

你可能感兴趣的文章
iOS开发中遇到的问题整理 (一)
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>
MapReduce的 Speculative Execution机制
查看>>
大数据学习之路------借助HDP SANDBOX开始学习
查看>>
Hadoop基础学习:基于Hortonworks HDP
查看>>
为什么linux安装程序 都要放到/usr/local目录下
查看>>
Hive安装前扫盲之Derby和Metastore
查看>>
永久修改PATH环境变量的几种办法
查看>>