Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Event,Delegation

Event,Delegation

Scheduled Pinned Locked Moved C#
csharpcomgraphics
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abalfazl
    wrote on last edited by
    #1

    Hello [url]http://www.csharp-station.com/Tutorials/Lesson14.aspx\[/url\] [code] using System; using System.Drawing; using System.Windows.forms; // custom delegate public delegate void Startdelegate(); class Eventdemo : form { // custom event public event Startdelegate StartEvent; public Eventdemo() { Button clickMe = new Button(); clickMe.Parent = this; clickMe.Text = "Click Me"; clickMe.Location = new Point( (ClientSize.Width - clickMe.Width) /2, (ClientSize.Height - clickMe.Height)/2); // an EventHandler delegate is assigned // to the button's Click event clickMe.Click += new EventHandler(OnClickMeClicked); // our custom "Startdelegate" delegate is assigned // to our custom "StartEvent" event. StartEvent += new Startdelegate(OnStartEvent); // fire our custom event StartEvent(); } // this method is called when the "clickMe" button is pressed public void OnClickMeClicked(object sender, EventArgs ea) { MessageBox.Show("You Clicked My Button!"); } // this method is called when the "StartEvent" Event is fired public void OnStartEvent() { MessageBox.Show("I Just Started!"); } static void Main(string[] args) { Application.Run(new Eventdemo()); } } [/code] And this from :[url]http://www.codeguru.com/csharp/csharp/cs\_delegates/eventhandling/\[/url\] [code] using System; //Step 1 Create delegate object public delegate void MyHandler1(object sender,MyEventArgs e); public delegate void MyHandler2(object sender,MyEventArgs e); //Step 2 Create event handler methods class A{ public const string m_id="Class A"; public void OnHandler1(object sender,MyEventArgs e){ Console.WriteLine("I am in OnHandler1 and MyEventArgs is {0}", e.m_id); } public void OnHandler2(object sender,MyEventArgs e){ Console.WriteLine("I am in OnHandler2 and MyEventArgs is {0}", e.m_id); } //Step 3 create delegates, plug in the handler and register // with the object that will fire the events public A(B b){ MyHandler1 d1=new MyHandler1(OnHandler1); MyHandler2 d2=new MyHandler2(OnHandler2); b.Event1 +=d1; b.Event2 +=d2; } } //Step 4 Calls the encapsulated methods through the // delegates (fires events) class B{ public event MyHandler1 Ev

    A Mike HankeyM 3 Replies Last reply
    0
    • A abalfazl

      Hello [url]http://www.csharp-station.com/Tutorials/Lesson14.aspx\[/url\] [code] using System; using System.Drawing; using System.Windows.forms; // custom delegate public delegate void Startdelegate(); class Eventdemo : form { // custom event public event Startdelegate StartEvent; public Eventdemo() { Button clickMe = new Button(); clickMe.Parent = this; clickMe.Text = "Click Me"; clickMe.Location = new Point( (ClientSize.Width - clickMe.Width) /2, (ClientSize.Height - clickMe.Height)/2); // an EventHandler delegate is assigned // to the button's Click event clickMe.Click += new EventHandler(OnClickMeClicked); // our custom "Startdelegate" delegate is assigned // to our custom "StartEvent" event. StartEvent += new Startdelegate(OnStartEvent); // fire our custom event StartEvent(); } // this method is called when the "clickMe" button is pressed public void OnClickMeClicked(object sender, EventArgs ea) { MessageBox.Show("You Clicked My Button!"); } // this method is called when the "StartEvent" Event is fired public void OnStartEvent() { MessageBox.Show("I Just Started!"); } static void Main(string[] args) { Application.Run(new Eventdemo()); } } [/code] And this from :[url]http://www.codeguru.com/csharp/csharp/cs\_delegates/eventhandling/\[/url\] [code] using System; //Step 1 Create delegate object public delegate void MyHandler1(object sender,MyEventArgs e); public delegate void MyHandler2(object sender,MyEventArgs e); //Step 2 Create event handler methods class A{ public const string m_id="Class A"; public void OnHandler1(object sender,MyEventArgs e){ Console.WriteLine("I am in OnHandler1 and MyEventArgs is {0}", e.m_id); } public void OnHandler2(object sender,MyEventArgs e){ Console.WriteLine("I am in OnHandler2 and MyEventArgs is {0}", e.m_id); } //Step 3 create delegates, plug in the handler and register // with the object that will fire the events public A(B b){ MyHandler1 d1=new MyHandler1(OnHandler1); MyHandler2 d2=new MyHandler2(OnHandler2); b.Event1 +=d1; b.Event2 +=d2; } } //Step 4 Calls the encapsulated methods through the // delegates (fires events) class B{ public event MyHandler1 Ev

      A Offline
      A Offline
      abalfazl
      wrote on last edited by
      #2

      Hello From Programming C#, 4th Edition [code] namespace EventKeyword { // a class to hold the information about the event // in this case it will hold only information // available in the clock class, but could hold // additional state information public class TimeInfoEventArgs : EventArgs { public readonly int hour; public readonly int minute; public readonly int second; public TimeInfoEventArgs(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } } // our subject -- it is this class that other classes // will observe. This class publishes one event: // OnSecondChange. The observers subscribe to that event public class Clock { private int hour; private int minute; private int second; // the delegate the subscribers must implement public delegate void SecondChangeHandler ( object clock, TimeInfoEventArgs timeInformation ); // the keyword event controls access to the delegate public event SecondChangeHandler OnSecondChange; // set the clock running // it will raise an event for each new second public void Run( ) { for(;;) { // sleep 10 milliseconds Thread.Sleep(10); // get the current time System.DateTime dt = System.DateTime.Now; // if the second has changed // notify the subscribers if (dt.Second != second) { // create the TimeInfoEventArgs object // to pass to the subscriber TimeInfoEventArgs timeInformation = new TimeInfoEventArgs( dt.Hour,dt.Minute,dt.Second); // if anyone has subscribed, notify them if (OnSecondChange != null) { OnSecondChange( this,timeInformation); } } // update the state this.second = dt.Second; this.minute = dt.Minute; this.hour = dt.Hour; } } } // an observer. DisplayClock subscribes to the // clock's events. The job of DisplayClock is // to display the current time public class DisplayClock { // given a clock, subscribe to // its SecondChangeHandler event public void Subscribe(Clock theClock) { theClock.OnSecondChange += new Clock.SecondChangeHandler(TimeHasChanged); } // the m

      A 1 Reply Last reply
      0
      • A abalfazl

        Hello [url]http://www.csharp-station.com/Tutorials/Lesson14.aspx\[/url\] [code] using System; using System.Drawing; using System.Windows.forms; // custom delegate public delegate void Startdelegate(); class Eventdemo : form { // custom event public event Startdelegate StartEvent; public Eventdemo() { Button clickMe = new Button(); clickMe.Parent = this; clickMe.Text = "Click Me"; clickMe.Location = new Point( (ClientSize.Width - clickMe.Width) /2, (ClientSize.Height - clickMe.Height)/2); // an EventHandler delegate is assigned // to the button's Click event clickMe.Click += new EventHandler(OnClickMeClicked); // our custom "Startdelegate" delegate is assigned // to our custom "StartEvent" event. StartEvent += new Startdelegate(OnStartEvent); // fire our custom event StartEvent(); } // this method is called when the "clickMe" button is pressed public void OnClickMeClicked(object sender, EventArgs ea) { MessageBox.Show("You Clicked My Button!"); } // this method is called when the "StartEvent" Event is fired public void OnStartEvent() { MessageBox.Show("I Just Started!"); } static void Main(string[] args) { Application.Run(new Eventdemo()); } } [/code] And this from :[url]http://www.codeguru.com/csharp/csharp/cs\_delegates/eventhandling/\[/url\] [code] using System; //Step 1 Create delegate object public delegate void MyHandler1(object sender,MyEventArgs e); public delegate void MyHandler2(object sender,MyEventArgs e); //Step 2 Create event handler methods class A{ public const string m_id="Class A"; public void OnHandler1(object sender,MyEventArgs e){ Console.WriteLine("I am in OnHandler1 and MyEventArgs is {0}", e.m_id); } public void OnHandler2(object sender,MyEventArgs e){ Console.WriteLine("I am in OnHandler2 and MyEventArgs is {0}", e.m_id); } //Step 3 create delegates, plug in the handler and register // with the object that will fire the events public A(B b){ MyHandler1 d1=new MyHandler1(OnHandler1); MyHandler2 d2=new MyHandler2(OnHandler2); b.Event1 +=d1; b.Event2 +=d2; } } //Step 4 Calls the encapsulated methods through the // delegates (fires events) class B{ public event MyHandler1 Ev

        A Offline
        A Offline
        abalfazl
        wrote on last edited by
        #3

        Hello about this: [code] public void OnHandler1(object sender,MyEventArgs e) [/code] May someone explain it? [url]http://www.codeguru.com/csharp/csharp/cs\_delegates/eventhandling/\[/url\]

        1 Reply Last reply
        0
        • A abalfazl

          Hello [url]http://www.csharp-station.com/Tutorials/Lesson14.aspx\[/url\] [code] using System; using System.Drawing; using System.Windows.forms; // custom delegate public delegate void Startdelegate(); class Eventdemo : form { // custom event public event Startdelegate StartEvent; public Eventdemo() { Button clickMe = new Button(); clickMe.Parent = this; clickMe.Text = "Click Me"; clickMe.Location = new Point( (ClientSize.Width - clickMe.Width) /2, (ClientSize.Height - clickMe.Height)/2); // an EventHandler delegate is assigned // to the button's Click event clickMe.Click += new EventHandler(OnClickMeClicked); // our custom "Startdelegate" delegate is assigned // to our custom "StartEvent" event. StartEvent += new Startdelegate(OnStartEvent); // fire our custom event StartEvent(); } // this method is called when the "clickMe" button is pressed public void OnClickMeClicked(object sender, EventArgs ea) { MessageBox.Show("You Clicked My Button!"); } // this method is called when the "StartEvent" Event is fired public void OnStartEvent() { MessageBox.Show("I Just Started!"); } static void Main(string[] args) { Application.Run(new Eventdemo()); } } [/code] And this from :[url]http://www.codeguru.com/csharp/csharp/cs\_delegates/eventhandling/\[/url\] [code] using System; //Step 1 Create delegate object public delegate void MyHandler1(object sender,MyEventArgs e); public delegate void MyHandler2(object sender,MyEventArgs e); //Step 2 Create event handler methods class A{ public const string m_id="Class A"; public void OnHandler1(object sender,MyEventArgs e){ Console.WriteLine("I am in OnHandler1 and MyEventArgs is {0}", e.m_id); } public void OnHandler2(object sender,MyEventArgs e){ Console.WriteLine("I am in OnHandler2 and MyEventArgs is {0}", e.m_id); } //Step 3 create delegates, plug in the handler and register // with the object that will fire the events public A(B b){ MyHandler1 d1=new MyHandler1(OnHandler1); MyHandler2 d2=new MyHandler2(OnHandler2); b.Event1 +=d1; b.Event2 +=d2; } } //Step 4 Calls the encapsulated methods through the // delegates (fires events) class B{ public event MyHandler1 Ev

          Mike HankeyM Offline
          Mike HankeyM Offline
          Mike Hankey
          wrote on last edited by
          #4

          Custom EventArgs are used when you need to pass additional information to the subscriber. You don't use custom event args in cases where you just want to know something happened such as a button click. You know its a button and its been clicked. You use custom event args if you want more info about the event say if you want to know the background color of the button. public class MyEventArgs : EventArgs { public Color btnColor; public MyEventArgs(Color color) { btnColor = color; } } then when you create; if (myHandler != null) myHandler(this, new MyEventArgs(button1.BackColor); Hope this helps Mike

          Everybody gotta be somebody

          1 Reply Last reply
          0
          • A abalfazl

            Hello From Programming C#, 4th Edition [code] namespace EventKeyword { // a class to hold the information about the event // in this case it will hold only information // available in the clock class, but could hold // additional state information public class TimeInfoEventArgs : EventArgs { public readonly int hour; public readonly int minute; public readonly int second; public TimeInfoEventArgs(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } } // our subject -- it is this class that other classes // will observe. This class publishes one event: // OnSecondChange. The observers subscribe to that event public class Clock { private int hour; private int minute; private int second; // the delegate the subscribers must implement public delegate void SecondChangeHandler ( object clock, TimeInfoEventArgs timeInformation ); // the keyword event controls access to the delegate public event SecondChangeHandler OnSecondChange; // set the clock running // it will raise an event for each new second public void Run( ) { for(;;) { // sleep 10 milliseconds Thread.Sleep(10); // get the current time System.DateTime dt = System.DateTime.Now; // if the second has changed // notify the subscribers if (dt.Second != second) { // create the TimeInfoEventArgs object // to pass to the subscriber TimeInfoEventArgs timeInformation = new TimeInfoEventArgs( dt.Hour,dt.Minute,dt.Second); // if anyone has subscribed, notify them if (OnSecondChange != null) { OnSecondChange( this,timeInformation); } } // update the state this.second = dt.Second; this.minute = dt.Minute; this.hour = dt.Hour; } } } // an observer. DisplayClock subscribes to the // clock's events. The job of DisplayClock is // to display the current time public class DisplayClock { // given a clock, subscribe to // its SecondChangeHandler event public void Subscribe(Clock theClock) { theClock.OnSecondChange += new Clock.SecondChangeHandler(TimeHasChanged); } // the m

            A Offline
            A Offline
            abalfazl
            wrote on last edited by
            #5

            Anyone knows the answer?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups