zoukankan      html  css  js  c++  java
  • c# 事件示例

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

    /// <summary>
    /// 自定义缩略图控件
    /// </summary>
    public  class ctlPictureBox
    {
        public delegate void PicClickHandler(object sender, PictureArgs e);
        /// <summary>
        /// 定义单击事件
        /// </summary>
        public event PicClickHandler PicClick;

        protected virtual void OnPictureClick(PictureArgs e)
        {//事件触发方法
            if (PicClick != null)
            {//判断事件是否为空
                PicClick(this, e);//触发事件
            }
        }
      //激发事件
        private void pbImg_Click(object sender, EventArgs e)
        {
            PictureArgs args = new PictureArgs();
            args.FileName = fileName;
            OnPictureClick(args);
        }

    }

       
    public class PictureArgs : EventArgs
    {
        private string filename = "";
        /// <summary>
        /// 文件名称
        /// </summary>
        public string FileName
        {
            get { return filename; }
            set { filename = value; }
        }
    }

       

    示例2

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

    namespace test
    {
     public partial class MyControl: UserControl
     {
      public MyControl()
      {
       InitializeComponent();
      }

            public delegate int MyDelegate(string str);

            public MyDelegate ClickHandler = null;


            private void button1_Click(object sender, EventArgs e)
            {
                if (ClickHandler != null)
                {
                    ClickHandler("hello");
                }
            }
     }
    }

    调用方法

    public int Test(string str)
            {
                MessageBox.Show(str);
                return 1;
            }

            public int Test1(string str)
            {
                MessageBox.Show(str +"," + str);
                return 1;
            }


            private void Form1_Load(object sender, EventArgs e)
            {
                myControl1.ClickHandler += Test;
                myControl1.ClickHandler += Test1;
             
              //  myControl1.ClickHandler -= Test1;
            }

  • 相关阅读:
    安装 Java 开发工具包JDK(Windows版本)
    在sublime text 3中让.vue文件的内容变成彩色
    iOS之禁止所有输入法的表情
    iOS之UIButton扩大按钮的响应区域
    iOS之利用腾讯Bugly程序调试,测试代码bug、卡顿等情况
    iOS之在本地搭建IPv6环境测试你的app
    iOS之让UISearchBar搜索图标和placeholder靠左显示
    iOS之限制TextField的输入长度
    iOS之oc与html之间的交互(oc中调用js的方法)
    iOS之面试题:腾讯三次面试以及参考思路
  • 原文地址:https://www.cnblogs.com/yg_zhang/p/1764731.html
Copyright © 2011-2022 走看看