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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Event » Key EventScreenshots 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.