[C#]capture des événements
voilà, je veux capturer l'événement d'un objet à partir d'un autre objet
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| using System;
using System.Collections.Generic;
using System.Text;
namespace EventsCS
{
class myEvent : EventArgs
{
private string myEventText = null;
public myEvent(string theEventText)
{
if (theEventText == null) throw new NullReferenceException();
myEventText = theEventText;
}
public string EventText
{
get { return this.myEventText; }
}
}
public delegate void myEventHandler(object sender, myEvent e);
class Source
{
public event myEventHandler OnTextChanged;
private string myStr;
public Source()
{
myStr = "";
}
public void setStr(string s)
{
if (s == null) throw new NullReferenceException();
myStr = s;
myEvent e = new myEvent("nouvel �v�nement\t" + s);
if (e != null)
if (OnTextChanged != null)
OnTextChanged(this, e);
}
}
class Receiver
{
public event myEventHandler OnTextChanged;
public Receiver()
{
}
public void subscribe(Source s)
{
s.OnTextChanged += new myEventHandler(Receiver_OnTextChanged);
}
public void Receiver_OnTextChanged(string s)
{
System.Console.WriteLine(s);
}
}
class Program
{
static void Main(string[] args)
{
Source src = new Source();
Receiver rcv = new Receiver();
string s = "coucou 1";
src.setStr(s);
s = "coucou 2";
src.setStr(s);
s = "coucou 3";
src.setStr(s);
}
}
} |
:merci: