Text Changed event


   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
    private System.Windows.Forms.GroupBox GroupBox1;
    private System.Windows.Forms.Label Label4;
    private System.Windows.Forms.Label Label1;
    private System.Windows.Forms.PictureBox pic;
    private System.Windows.Forms.TextBox txt;
    private System.Windows.Forms.Button cmd;
    private System.Windows.Forms.Label Label2;
    private System.Windows.Forms.Label Label3;
    private System.Windows.Forms.ListBox eventLogList;

    public Form1() {
        InitializeComponent();
    }
    private void Log(String data)
    {
        eventLogList.Items.Add(data);
        int itemsPerPage = (int)(eventLogList.Height / eventLogList.ItemHeight);
        eventLogList.TopIndex = eventLogList.Items.Count - itemsPerPage;
    }

    private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
    }

    private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        Log("Key Press: " + e.KeyChar.ToString());
    }

    private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
    }

    private void txt_TextChanged(object sender, System.EventArgs e)
    {
        Log("Changed: " + " Text is: " + txt.Text);
    }

    private void pic_MouseEnter(object sender, System.EventArgs e)
    {
        Log("Mouse Enter");
    }

    private void pic_MouseHover(object sender, System.EventArgs e)
    {
        Log("Mouse Hover");
    }

    private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Log("Mouse Down: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
    }

    private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Log("Mouse Up: X=" + e.X.ToString() + " Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
    }

    private void pic_Click(object sender, System.EventArgs e)
    {
        Log("Click");
    }

    private void pic_DoubleClick(object sender, System.EventArgs e)
    {
        Log("Double Click");
    }

    private void pic_MouseLeave(object sender, System.EventArgs e)
    {
        Log("Mouse Leave");
    }

    private void InitializeComponent()
    {
        this.GroupBox1 = new System.Windows.Forms.GroupBox();
        this.Label4 = new System.Windows.Forms.Label();
        this.Label1 = new System.Windows.Forms.Label();
        this.pic = new System.Windows.Forms.PictureBox();
        this.txt = new System.Windows.Forms.TextBox();
        this.cmd = new System.Windows.Forms.Button();
        this.Label2 = new System.Windows.Forms.Label();
        this.Label3 = new System.Windows.Forms.Label();
        this.eventLogList = new System.Windows.Forms.ListBox();
        this.GroupBox1.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pic)).BeginInit();
        this.SuspendLayout();
        // 
        // GroupBox1
        // 
        this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.GroupBox1.Controls.Add(this.Label4);
        this.GroupBox1.Controls.Add(this.Label1);
        this.GroupBox1.Controls.Add(this.pic);
        this.GroupBox1.Controls.Add(this.txt);
        this.GroupBox1.Controls.Add(this.cmd);
        this.GroupBox1.Controls.Add(this.Label2);
        this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
        this.GroupBox1.Location = new System.Drawing.Point(7, 0);
        this.GroupBox1.Name = "GroupBox1";
        this.GroupBox1.Size = new System.Drawing.Size(384, 148);
        this.GroupBox1.TabIndex = 12;
        this.GroupBox1.TabStop = false;
        // 
        // Label4
        // 
        this.Label4.Location = new System.Drawing.Point(92, 108);
        this.Label4.Name = "Label4";
        this.Label4.Size = new System.Drawing.Size(56, 16);
        this.Label4.TabIndex = 5;
        this.Label4.Text = "And here:";
        // 
        // Label1
        // 
        this.Label1.Location = new System.Drawing.Point(6, 24);
        this.Label1.Name = "Label1";
        this.Label1.Size = new System.Drawing.Size(144, 16);
        this.Label1.TabIndex = 2;
        this.Label1.Text = "Test keyboard events here:";
        // 
        // pic
        // 
        this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.pic.Location = new System.Drawing.Point(156, 48);
        this.pic.Name = "pic";
        this.pic.Size = new System.Drawing.Size(192, 48);
        this.pic.TabIndex = 3;
        this.pic.TabStop = false;
        this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
        this.pic.Click += new System.EventHandler(this.pic_Click);
        this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
        this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
        this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
        this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
        // 
        // txt
        // 
        this.txt.Location = new System.Drawing.Point(156, 20);
        this.txt.Name = "txt";
        this.txt.Size = new System.Drawing.Size(192, 21);
        this.txt.TabIndex = 1;
        this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
        this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
        this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
        this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
        // 
        // cmd
        // 
        this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
        this.cmd.Location = new System.Drawing.Point(156, 100);
        this.cmd.Name = "cmd";
        this.cmd.Size = new System.Drawing.Size(88, 28);
        this.cmd.TabIndex = 4;
        this.cmd.Text = "Button1";
        this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
        this.cmd.Click += new System.EventHandler(this.pic_Click);
        this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
        this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
        this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
        this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
        // 
        // Label2
        // 
        this.Label2.Location = new System.Drawing.Point(20, 52);
        this.Label2.Name = "Label2";
        this.Label2.Size = new System.Drawing.Size(128, 16);
        this.Label2.TabIndex = 2;
        this.Label2.Text = "Test mouse events here:";
        // 
        // Label3
        // 
        this.Label3.Location = new System.Drawing.Point(23, 100);
        this.Label3.Name = "Label3";
        this.Label3.Size = new System.Drawing.Size(64, 24);
        this.Label3.TabIndex = 11;
        this.Label3.Text = "Label3";
        // 
        // eventLogList
        // 
        this.eventLogList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.eventLogList.FormattingEnabled = true;
        this.eventLogList.IntegralHeight = false;
        this.eventLogList.Location = new System.Drawing.Point(7, 156);
        this.eventLogList.Name = "eventLogList";
        this.eventLogList.Size = new System.Drawing.Size(384, 212);
        this.eventLogList.TabIndex = 10;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(399, 374);
        this.Controls.Add(this.GroupBox1);
        this.Controls.Add(this.Label3);
        this.Controls.Add(this.eventLogList);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "Form1";
        this.Text = "Event Tracker";
        this.GroupBox1.ResumeLayout(false);
        this.GroupBox1.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pic)).EndInit();
        this.ResumeLayout(false);

    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

}



           
          


A simple text editor


   

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

public class MenuDialog : Form {
  TextBox text = new TextBox();

  public MenuDialog() {
    Size = new Size(500,200);

    text.Size = new Size(490,190);
    text.Multiline = true;
    text.ScrollBars = ScrollBars.Both;
    text.WordWrap = false;
    text.Location = new Point(5,5);

    MenuItem fileMenu = new MenuItem("File");
    MenuItem open = new MenuItem("Open");
    open.Shortcut = Shortcut.CtrlO;
    MenuItem save = new MenuItem("Save");
    save.Shortcut = Shortcut.CtrlS;
    fileMenu.MenuItems.Add(open);
    fileMenu.MenuItems.Add(save);

    MenuItem formatMenu = new MenuItem("Format");
    MenuItem font = new MenuItem("Font");
    font.Shortcut = Shortcut.CtrlF;
    formatMenu.MenuItems.Add(font);
     
    MainMenu bar = new MainMenu();
    Menu = bar;
    bar.MenuItems.Add(fileMenu);
    bar.MenuItems.Add(formatMenu);

    Controls.Add(text);

    open.Click += new EventHandler(Open_Click);
    save.Click += new EventHandler(Save_Click);
    font.Click += new EventHandler(Font_Click); 
  }
  
  protected void Open_Click(Object sender, EventArgs e) {
    OpenFileDialog o = new OpenFileDialog();
    if(o.ShowDialog() == DialogResult.OK) {
      Stream file = o.OpenFile();
      StreamReader reader = new StreamReader(file);
      char[] data = new char[file.Length];
      reader.ReadBlock(data,0,(int)file.Length);
      text.Text = new String(data);  
      reader.Close();
    }
  }

  protected void Save_Click(Object sender, EventArgs e) {
    SaveFileDialog s = new SaveFileDialog();
    if(s.ShowDialog() == DialogResult.OK) {
      StreamWriter writer = new StreamWriter(s.OpenFile());
      writer.Write(text.Text);
      writer.Close();
    }
  }
  protected void Font_Click(Object sender, EventArgs e) {
    FontDialog f = new FontDialog();
    if(f.ShowDialog() == DialogResult.OK) 
      text.Font = f.Font;
  }

  public static void Main() {
    Application.Run(new MenuDialog());
  }
}

           
          


Convert TextBox input to double value


   

using System;
using System.Drawing;
using System.Windows.Forms;
public class EnterPrice : Form {
  private Button enter = new Button();
  private Label answer = new Label();
  private TextBox text = new TextBox( );

  public EnterPrice( ) {
    enter.Text = "Enter Price";
    text.Text = "";
    answer.Text = "";

    Size = new Size(300,200);
    answer.Size = new Size(200,50);

    enter.Location = new Point(30 + enter.Width, 30);
    text.Location = new Point (40 + enter.Width + enter.Width, 30);
    answer.Location = new Point(20, 60);

    AcceptButton = enter;

    Controls.Add(text);
    Controls.Add(answer);
    Controls.Add(enter);

    enter.Click += new EventHandler(Enter_Click);
  }

  protected void Enter_Click(Object sender, EventArgs e) {
    try{
    Console.WriteLine(Double.Parse(text.Text));
    }catch(Exception){
    }
    text.Text = "";
    text.Focus();
  }
  static void Main() {
    Application.Run(new EnterPrice());
  }
}

           
          


All cap text textbox


   



    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

  public class TextForm : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button btnPasswordDecoderRing;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox passwordBox;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox capsOnlyBox;
    private System.Windows.Forms.Button btnGetMultiLineText;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox multiLineBox;

        public TextForm()
        {
            InitializeComponent();
        }

    private void InitializeComponent()
    {
      this.capsOnlyBox = new System.Windows.Forms.TextBox();
      this.multiLineBox = new System.Windows.Forms.TextBox();
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.passwordBox = new System.Windows.Forms.TextBox();
      this.btnGetMultiLineText = new System.Windows.Forms.Button();
      this.btnPasswordDecoderRing = new System.Windows.Forms.Button();
      this.label3 = new System.Windows.Forms.Label();
      this.capsOnlyBox.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
      this.capsOnlyBox.Location = new System.Drawing.Point(14, 176);
      this.capsOnlyBox.Size = new System.Drawing.Size(120, 20);
      this.capsOnlyBox.TabIndex = 3;
      this.multiLineBox.AcceptsReturn = true;
      this.multiLineBox.AcceptsTab = true;
      this.multiLineBox.Location = new System.Drawing.Point(152, 8);
      this.multiLineBox.Multiline = true;
      this.multiLineBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
      this.multiLineBox.Size = new System.Drawing.Size(240, 104);
      this.multiLineBox.TabIndex = 0;
      this.multiLineBox.Text = "Get text from multiline textbox";
      this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F);
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Size = new System.Drawing.Size(136, 56);
      this.label1.TabIndex = 1;
      this.label1.Text = "this is a multiline text box";
      this.label2.Font = new System.Drawing.Font("Comic Sans MS", 12F);
      this.label2.Location = new System.Drawing.Point(14, 144);
      this.label2.Size = new System.Drawing.Size(106, 24);
      this.label2.TabIndex = 4;
      this.label2.Text = "Caps only!!";
      this.passwordBox.Location = new System.Drawing.Point(160, 176);
      this.passwordBox.PasswordChar = '$';
      this.passwordBox.Size = new System.Drawing.Size(232, 20);
      this.passwordBox.TabIndex = 5;
      this.passwordBox.Text = "password";
      this.btnGetMultiLineText.Location = new System.Drawing.Point(13, 72);
      this.btnGetMultiLineText.Size = new System.Drawing.Size(120, 32);
      this.btnGetMultiLineText.TabIndex = 2;
      this.btnGetMultiLineText.Text = "Get Text";
      this.btnGetMultiLineText.Click += new System.EventHandler(this.btnGetMultiLineText_Click);
      this.btnPasswordDecoderRing.Location = new System.Drawing.Point(280, 144);
      this.btnPasswordDecoderRing.Size = new System.Drawing.Size(112, 24);
      this.btnPasswordDecoderRing.TabIndex = 7;
      this.btnPasswordDecoderRing.Text = "Decode Password";
      this.btnPasswordDecoderRing.Click += new System.EventHandler(this.btnPasswordDecoderRing_Click);
      this.label3.Font = new System.Drawing.Font("Comic Sans MS", 12F);
      this.label3.Location = new System.Drawing.Point(152, 144);
      this.label3.Size = new System.Drawing.Size(120, 24);
      this.label3.TabIndex = 6;
      this.label3.Text = "Password Box";
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(408, 221);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnPasswordDecoderRing,
                                      this.label3,
                                      this.passwordBox,
                                      this.label2,
                                      this.capsOnlyBox,
                                      this.btnGetMultiLineText,
                                      this.label1,
                                      this.multiLineBox});
      this.Text = "TextBox Types";

    }

    protected void btnPasswordDecoderRing_Click (object sender, System.EventArgs e)
    {
      MessageBox.Show(passwordBox.Text, "Your password is:");
    }

    protected void btnGetMultiLineText_Click (object sender, System.EventArgs e)
    {
      MessageBox.Show(multiLineBox.Text, "Here is your text");
    }

        public static void Main(string[] args) 
        {
            Application.Run(new TextForm());
        }
    }

           
          


Data Checker


   

/*
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.Data.SqlClient;
using System.Threading;


namespace DataChecker
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class DataChecker : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox txtCoffeePrice;
        private System.Windows.Forms.TextBox txtTeaPrice;
        private System.Windows.Forms.TextBox txtGoldPrice;
        private System.Windows.Forms.Label lblCoffeePrice;
        private System.Windows.Forms.Label lblTeaPrice;
        private System.Windows.Forms.Label lblGoldPrice;

        private int lastCoffeePrice = 0;
        private int lastTeaPrice = 0;
        private int lastGoldPrice = 0;
        private int allUpdates = 0;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        private System.Threading.Timer t1 = null;
        private System.Threading.Timer t2 = null;
        private System.Threading.Timer t3 = null;

        public DataChecker()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

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

            t1 = new System.Threading.Timer(new TimerCallback(Timer_Callback), &#039;C&#039;, 0, 500);
            t2 = new System.Threading.Timer(new TimerCallback(Timer_Callback), &#039;T&#039;, 1, 500);
            t3 = new System.Threading.Timer(new TimerCallback(Timer_Callback), &#039;G&#039;, 2, 500);        

            /*WaitOrTimerCallback wotc = new WaitOrTimerCallback(GetData);
            AutoResetEvent are = new AutoResetEvent(false);
            ThreadPool.RegisterWaitForSingleObject(are, new WaitOrTimerCallback(GetData), &#039;C&#039;, 500, false);*/

        }

        protected void Timer_Callback(object state)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(GetData), state);
        }


        int retVal;
        private void GetData(object type)
        {
            char priceType = (char)type;
            string sql = null;

            sql = "SELECT Price FROM tblPrices WHERE Type=&#039;"+priceType.ToString()+"&#039;";

            SqlConnection cn = new SqlConnection("Server=localhost; Database=Prices; Integrated Security=SSPI");
            cn.Open();

            SqlCommand cmd = new SqlCommand(sql, cn);

            
            lock(this)
            {
                retVal = (int)cmd.ExecuteScalar(); 
                switch(priceType) 
                {
                    case &#039;C&#039;:
                        lastCoffeePrice = Convert.ToInt32(txtCoffeePrice.Text);
                        txtCoffeePrice.Text = retVal.ToString();
                        break;
                    case &#039;T&#039;:
                        lastTeaPrice = Convert.ToInt32(txtTeaPrice.Text);
                        txtTeaPrice.Text = retVal.ToString();
                        break;
                    case &#039;G&#039;:
                        lastGoldPrice = Convert.ToInt32(txtGoldPrice.Text);
                        txtGoldPrice.Text = retVal.ToString();
                        break;
                }
            }
            

            allUpdates++;
            cn.Close();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #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.txtCoffeePrice = new System.Windows.Forms.TextBox();
            this.txtTeaPrice = new System.Windows.Forms.TextBox();
            this.txtGoldPrice = new System.Windows.Forms.TextBox();
            this.lblCoffeePrice = new System.Windows.Forms.Label();
            this.lblTeaPrice = new System.Windows.Forms.Label();
            this.lblGoldPrice = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // txtCoffeePrice
            // 
            this.txtCoffeePrice.Location = new System.Drawing.Point(72, 24);
            this.txtCoffeePrice.Name = "txtCoffeePrice";
            this.txtCoffeePrice.TabIndex = 0;
            this.txtCoffeePrice.Text = "0";
            // 
            // txtTeaPrice
            // 
            this.txtTeaPrice.Location = new System.Drawing.Point(192, 24);
            this.txtTeaPrice.Name = "txtTeaPrice";
            this.txtTeaPrice.TabIndex = 1;
            this.txtTeaPrice.Text = "0";
            // 
            // txtGoldPrice
            // 
            this.txtGoldPrice.Location = new System.Drawing.Point(312, 24);
            this.txtGoldPrice.Name = "txtGoldPrice";
            this.txtGoldPrice.TabIndex = 2;
            this.txtGoldPrice.Text = "0";
            // 
            // lblCoffeePrice
            // 
            this.lblCoffeePrice.Location = new System.Drawing.Point(72, 0);
            this.lblCoffeePrice.Name = "lblCoffeePrice";
            this.lblCoffeePrice.TabIndex = 3;
            this.lblCoffeePrice.Text = "Coffee Price";
            // 
            // lblTeaPrice
            // 
            this.lblTeaPrice.Location = new System.Drawing.Point(192, 0);
            this.lblTeaPrice.Name = "lblTeaPrice";
            this.lblTeaPrice.TabIndex = 4;
            this.lblTeaPrice.Text = "Tea Price";
            // 
            // lblGoldPrice
            // 
            this.lblGoldPrice.Location = new System.Drawing.Point(320, 0);
            this.lblGoldPrice.Name = "lblGoldPrice";
            this.lblGoldPrice.TabIndex = 5;
            this.lblGoldPrice.Text = "Gold Price";
            // 
            // DataChecker
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(424, 54);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lblGoldPrice,
                                                                          this.lblTeaPrice,
                                                                          this.lblCoffeePrice,
                                                                          this.txtGoldPrice,
                                                                          this.txtTeaPrice,
                                                                          this.txtCoffeePrice});
            this.MaximizeBox = false;
            this.Name = "DataChecker";
            this.Text = "Data Checker";
            this.ResumeLayout(false);

        }
        #endregion

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

        
    }
}


           
          


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 Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            la.LoginAudit -= new LoginAuditInserter.LoginAuditHandler(AddAuditEntry);
            la.LoginAudit -= new LoginAuditInserter.LoginAuditHandler(AddEventLogEntry);

            base.Dispose( disposing );
        }

        #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(8, 24);
            this.txtUsername.Name = "txtUsername";
            this.txtUsername.Size = new System.Drawing.Size(152, 20);
            this.txtUsername.TabIndex = 0;
            this.txtUsername.Text = "";
            // 
            // btnLogin
            // 
            this.btnLogin.Location = new System.Drawing.Point(184, 24);
            this.btnLogin.Name = "btnLogin";
            this.btnLogin.Size = new System.Drawing.Size(96, 23);
            this.btnLogin.TabIndex = 1;
            this.btnLogin.Text = "Login";
            this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
            // 
            // UserEvents
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 78);
            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));
        }
    }


}

           
          


System Tray App



   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace SystemTrayApp
{
  /// <summary>
  /// Summary description for SystemTrayApp.
  /// </summary>
  public class SystemTrayApp : System.Windows.Forms.Form
  {
    internal System.Windows.Forms.Label Label1;
    internal System.Windows.Forms.Button cmdClose;
    internal System.Windows.Forms.ListBox lstFiles;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public SystemTrayApp()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

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

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #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.Label1 = new System.Windows.Forms.Label();
      this.cmdClose = new System.Windows.Forms.Button();
      this.lstFiles = new System.Windows.Forms.ListBox();
      this.SuspendLayout();
      // 
      // Label1
      // 
      this.Label1.Location = new System.Drawing.Point(10, 7);
      this.Label1.Name = "Label1";
      this.Label1.Size = new System.Drawing.Size(140, 16);
      this.Label1.TabIndex = 5;
      this.Label1.Text = "Recently created files:";
      // 
      // cmdClose
      // 
      this.cmdClose.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | 
                      System.Windows.Forms.AnchorStyles.Right);
      this.cmdClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.cmdClose.Location = new System.Drawing.Point(162, 203);
      this.cmdClose.Name = "cmdClose";
      this.cmdClose.Size = new System.Drawing.Size(88, 24);
      this.cmdClose.TabIndex = 4;
      this.cmdClose.Text = "Close";
      this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
      // 
      // lstFiles
      // 
      this.lstFiles.Anchor = (((System.Windows.Forms.AnchorStyles.Top | 
                         System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right);
      this.lstFiles.IntegralHeight = false;
      this.lstFiles.Location = new System.Drawing.Point(10, 27);
      this.lstFiles.Name = "lstFiles";
      this.lstFiles.Size = new System.Drawing.Size(240, 168);
      this.lstFiles.TabIndex = 3;
      // 
      // SystemTrayApp
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
      this.ClientSize = new System.Drawing.Size(260, 234);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                this.Label1,
                 this.cmdClose,
                  this.lstFiles});
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, 
                         System.Drawing.FontStyle.Regular, 
                         System.Drawing.GraphicsUnit.Point, 
                         ((System.Byte)(0)));
      this.Name = "SystemTrayApp";
      this.Text = "SystemTrayApp";
      this.ResumeLayout(false);

    }
    #endregion

    private void cmdClose_Click(object sender, System.EventArgs e)
    {
      this.Close();
    }

    public void FillList(ArrayList list)
    {
      lstFiles.DataSource = list;
    }

  }
}
//====================================================================
//====================================================================

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;


namespace SystemTrayApp
{
  public class App
  {
    // Define the system tray icon control.
    private NotifyIcon appIcon = new NotifyIcon();

    // Define the menu.
    private ContextMenu sysTrayMenu = new ContextMenu();
    private MenuItem displayFiles = new MenuItem("Display New Files");
    private MenuItem exitApp = new MenuItem("Exit");

    // Define the file system watcher, and a list to store filenames.
    private FileSystemWatcher watch = new FileSystemWatcher();
    private ArrayList newFiles = new ArrayList();

    public void Start()
    {
      // Configure the system tray icon.
      Icon ico = new Icon("icon.ico");
      appIcon.Icon = ico;
      appIcon.Text = "My .NET Application";


       // Place the menu items in the menu.
       sysTrayMenu.MenuItems.Add(displayFiles);
      sysTrayMenu.MenuItems.Add(exitApp);
      appIcon.ContextMenu = sysTrayMenu;

      // Show the system tray icon.
      appIcon.Visible = true;

      // Hook up the file watcher.
      watch.Path = "c:";
      watch.IncludeSubdirectories = true;
      watch.EnableRaisingEvents = true;

      // Attach event handlers.
      watch.Created += new FileSystemEventHandler(FileCreated);
      displayFiles.Click += new EventHandler(DisplayFiles);
      exitApp.Click += new EventHandler(ExitApp);

    }

    private void FileCreated(object sender, System.IO.FileSystemEventArgs e)
    {
      newFiles.Add(e.Name);
    }
    private void ExitApp(object sender, System.EventArgs e)
    {
      Application.Exit();
    }

    private void DisplayFiles(object sender, System.EventArgs e)
    {
      FileList frmFileList = new FileList();
      frmFileList.FillList(newFiles);
      frmFileList.Show();
    }

    public static void Main()
    {
      App app = new App();
      app.Start();

      // Because no forms are being displayed, you need this 
      // statement to stop the application from automatically ending.
      Application.Run();
    }

  }


}


           
          


SystemTrayApp.zip( 26 k)