C#メモ

[個人メモ] [アイデア] [仕事メモ] [Ruby] [Qt] [TstLink]
 | 

2017-05-19

Event

System.EventHandler

読み解くと、EventHandlerは、void型で、引数object型とEventArgs型をもつdelegateとある

http://gomocool.net/gomokulog/?p=255

EventArgs型

http://dobon.net/vb/dotnet/vb2cs/event.html

Eventの例


namespace WindowsFormsApplication1
{
    public delegate void myDelegate(string myTime);
    public class Class1
    {
        public event myDelegate myEvent;

        public void myMethod()
        {
            string msg = DateTime.Now.ToString("HH:mm:ss");
            myEvent(msg);
        }
    }
}

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Class1 myClass = new Class1();

        public Form1()
        {
            InitializeComponent();
        }

        private void myClass_myEvent(string updateTime)
        {
            this.label1.Text = updateTime;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myClass.myEvent+= new myDelegate(myClass_myEvent);
            myClass.myMethod();
        }
    }
}

Event

System.EventHandler

読み解くと、EventHandlerは、void型で、引数object型とEventArgs型をもつdelegateとある

http://gomocool.net/gomokulog/?p=255

EventArgs型

http://dobon.net/vb/dotnet/vb2cs/event.html

Eventの例


namespace WindowsFormsApplication1
{
    public delegate void myDelegate(string myTime);
    public class Class1
    {
        public event myDelegate myEvent;

        public void myMethod()
        {
            string msg = DateTime.Now.ToString("HH:mm:ss");
            myEvent(msg);
        }
    }
}

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Class1 myClass = new Class1();

        public Form1()
        {
            InitializeComponent();
        }

        private void myClass_myEvent(string updateTime)
        {
            this.label1.Text = updateTime;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myClass.myEvent+= new myDelegate(myClass_myEvent);
            myClass.myMethod();
        }
    }
}

ゲスト



トラックバック - http://swproject.g.hatena.ne.jp/garyo/20170519
 |