User Events : TextBox « GUI Windows Form « 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » TextBoxScreenshots 
User Events
User Events

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace UserEvents
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class UserEvents : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox txtUsername;
        private System.Windows.Forms.Button btnLogin;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        LoginAuditInserter la = new LoginAuditInserter();
        public UserEvents()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //

            
            la.LoginAudit += new LoginAuditInserter.LoginAuditHandler(AddAuditEntry);
            la.LoginAudit += new LoginAuditInserter.LoginAuditHandler(AddEventLogEntry);
        }

        static public void AddAuditEntry(string username)
        {
            System.Diagnostics.Debug.WriteLine(username);
        }

        static public void AddEventLogEntry(string username)
        {
            string applicationName = "Login Audit";
            EventLog ev = new EventLog("Application");
            ev.Source = applicationName;
            ev.WriteEntry("Login Attempted.", EventLogEntryType.Information);
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Disposebool disposing )
        {
            ifdisposing )
            {
                if (components != null
                {
                    components.Dispose();
                }
            }
            la.LoginAudit -= new LoginAuditInserter.LoginAuditHandler(AddAuditEntry);
            la.LoginAudit -= new LoginAuditInserter.LoginAuditHandler(AddEventLogEntry);

            base.Disposedisposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.txtUsername = new System.Windows.Forms.TextBox();
            this.btnLogin = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // txtUsername
            // 
            this.txtUsername.Location = new System.Drawing.Point(824);
            this.txtUsername.Name = "txtUsername";
            this.txtUsername.Size = new System.Drawing.Size(15220);
            this.txtUsername.TabIndex = 0;
            this.txtUsername.Text = "";
            // 
            // btnLogin
            // 
            this.btnLogin.Location = new System.Drawing.Point(18424);
            this.btnLogin.Name = "btnLogin";
            this.btnLogin.Size = new System.Drawing.Size(9623);
            this.btnLogin.TabIndex = 1;
            this.btnLogin.Text = "Login";
            this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
            // 
            // UserEvents
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            this.ClientSize = new System.Drawing.Size(29278);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.btnLogin,
                                                                          this.txtUsername});
            this.MaximizeBox = false;
            this.Name = "UserEvents";
            this.Text = "Login";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new UserEvents());
        }

        private void btnLogin_Click(object sender, System.EventArgs e)
        {
            la.AddAuditEntry(txtUsername.Text);     
        }
    }

    public class LoginAuditInserter
    {
        public delegate void LoginAuditHandler(string username);

        private AccessorContainer container = new AccessorContainer();
        private static int key = 0;

        public event LoginAuditHandler LoginAudit
        {
            [MethodImpl(MethodImplOptions.Synchronized)]
            add
            {
                container.Add(key, value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            remove
            {
                container.Remove(key, value);
            }
        }

        protected void OnLoginAudit(string username)
        {
            LoginAuditHandler loginAudit = (LoginAuditHandler)container.Get(key);
            if(username!=null)
            {
                loginAudit(username);
            }
        }

        public void AddAuditEntry(string username)
        {
            OnLoginAudit(username);
        }
    }

    public class AccessorContainer
    {
        private ArrayList arrayAccessor = new ArrayList();

        public Delegate Get(int key)
        {
            return ((Delegate)arrayAccessor[key]);
        }

        public void Add(int key, Delegate ptr)
        {
            try
            {
                arrayAccessor[key= Delegate.Combine((Delegate)arrayAccessor[key], ptr);
            }
            catch(ArgumentOutOfRangeException)
            {
                arrayAccessor.Add(ptr);
            }
        }

        public void Remove(int key, Delegate ptr)
        {
            arrayAccessor.Remove(Delegate.Remove((Delegate)arrayAccessor[key], ptr));
        }
    }


}

           
       
Related examples in the same category
1.Add ScrollBars to TextBox
2.new TextBox(), Localtion, Name, TabIndex, Text
3.TextBox locationTextBox location
4.Keyboard event and TextBox
5.Get value from TextBox
6.Text Changed eventText Changed event
7.A simple text editorA simple text editor
8.Convert TextBox input to double valueConvert TextBox input to double value
9.All cap text textboxAll cap text textbox
10.Data CheckerData Checker
11.TextBox and button on formTextBox and button on form
12.TextBox and ListBoxTextBox and ListBox
13.Label, TextBox and ButtonLabel, TextBox and Button
14.TextBox DemoTextBox Demo
15.Simple Editor based on TextBox
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.