Thread: Add cutom event on form

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    35

    Add cutom event on form

    How I can append custom event on form like I do it on button:
    button1.Click += System. ... ?
    im from LMoldovaZ

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    One word: delegates.

    for example:

    Code:
    public delegate void OnChange(string data);
    You can pass a method that has a signature as follows to this delegate.

    Code:
    public void Bla(string d);
    So suppose you have a class as follows:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    
    namespace ConsoleTest {
    
    	public delegate void OnChange(string data);
    	
    	class EventTest {
    
    		public OnChange Update;
    
    		private Thread t;
    
    		private int[] ar;
    
    		private int fireEvent;
    
    		public EventTest(int members,int fireEventMember) {
    			ar = new int[members];
    
    			this.fireEvent = fireEventMember;
    			
    			t = new Thread(new ThreadStart(this.Run));
    			t.IsBackground = true;
    			t.Start();
    		}
    
    		public void Run() {
    			while (true) {
    				for (int i = 0; i < ar.Length; i++) {
    					if(i==this.fireEvent) {
    						this.Update(i.ToString());
    					}
    				}
    				Thread.Sleep(500);
    			}
    		}
    	}
    }
    Now each time i==this.fireEvent is true Update is called.

    Upto now all of this is pretty useless ... unless we do something with Update...

    So in our Program class we write.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleTest {
    	class Program {
    		static void Main(string[] args) {
    
    			Program p = new Program();
    
    			EventTest test = new EventTest(10, 5);
    			test.Update = new OnChange(p.Display);
    			test.Update += new OnChange(p.DisplaySecond);
    
    
    			Console.ReadLine();
    			test.Update -= new OnChange(p.DisplaySecond);
    
    			Console.ReadLine();
    		}
    
    		public void Display(string d) {
    			Console.WriteLine("From method 1: " +d);
    		}
    
    		public void DisplaySecond(string d) {
    			Console.WriteLine("From method 2: " + d);
    		}
    	}
    }
    So we actually give the meaning of what should happen in Program. Each time Update is called the methode Display should be executed.

    Subscribing and un- is done trough += and -= .

    If you dont understand any of this, search for some delegate tutorials....
    Last edited by GanglyLamb; 07-01-2006 at 03:11 AM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    I know about it, BUT i ask about Form`s custom event like OnLoad, which can be alerted by ME but not by OS or other things.
    or how i can by my own do it ?
    im from LMoldovaZ

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Sorry then I dont know what you are trying to achieve.

    which can be alerted by ME
    In my example this is done by calling:

    Code:
    this.Update(i.ToString());
    So sorry can't help you since I don't have a clue what you are talking about.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    35
    I think i found: Form.Events.AddHandler(), but I still want to found a few examples how to use it.
    im from LMoldovaZ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Event driven thread programming
    By jaxen in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2006, 08:46 AM
  2. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  3. Accessing main form from functions in other classes
    By pj_martins in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2004, 09:27 AM
  4. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 08:55 AM
  5. Can somebody test this code please
    By andy bee in forum C Programming
    Replies: 6
    Last Post: 10-09-2001, 03:08 PM