Event system explained in detail : Key Event « Event « C# / C Sharp






Event system explained in detail

using System;

namespace Demo
{
    //step 0. use event to simply complex interaction, independent data process module

    //step 1. declare a event signatrue
    public delegate void OnUserDeviceInsertedEvent(object sender, UserDeviceInsertedArgs e);

    //step 2. declare the event args
    public class UserDeviceInsertedArgs : EventArgs
    {
        public string DeviceData;
    }

    //step 3. declare a real work class which has 1 event field member and 1 working method member, in the method event will be triggered when condiation ready

    public class MagCard
    {
        public OnUserDeviceInsertedEvent OnDeviceInserted;
        public void StartingWork()
        {
            UserDeviceInsertedArgs data = new UserDeviceInsertedArgs();
            data.DeviceData = "Demo Data";
            if (OnDeviceInserted != null)
            {
                OnDeviceInserted(this, data);
            }
        }
    }

    //step 4. in main system, ord the event and supperly suitable process
    static class Program
    {
        /// <summary>
        /// ??????????
        /// </summary>
        [STAThread]
        static void Main()
        {
            MagCard machine = new MagCard();
            machine.OnDeviceInserted += new OnUserDeviceInsertedEvent(EventHandler_OnDeviceInserted);
            machine.StartingWork();
            Console.ReadKey();
        }

        private static void EventHandler_OnDeviceInserted(object sender, UserDeviceInsertedArgs e)
        {
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine(e.DeviceData);
            Console.ResetColor();
        }
    }
    //Coding by MoonKite,2010-June,Study C#
    //Thanks for Java2s,I learned a lot form this website.
}       
           
       








Related examples in the same category

1.Close Form with Pressing X key
2.Check KeyCode from KeyEventArgs
3.Shift key pressedShift key pressed
4.Control Key pressedControl Key pressed
5.Alt key pressedAlt key pressed
6.Get Async Key StateGet Async Key State
7.Get Key action informationGet Key action information
8.Displaying information about the key the user pressedDisplaying information about the key the user pressed
9.Displays a key pressed by the userDisplays a key pressed by the user
10.Full screen and KeyEvent and MouseEvent
11.Key PressKey Press
12.Key Timer Key Timer
13.Keyboard SampleKeyboard Sample