Events and form controls : event « GUI Windows Forms « C# / CSharp Tutorial






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

    public class Events : Form
    {
        public Events()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("I have been Clicked");
        }

        private void textBox1_MouseEnter(object sender, EventArgs e)
        {
            label1.Text = "Mouse Enters into the TextBox";            
        }

        private void textBox1_MouseLeave(object sender, EventArgs e)
        {
            label1.Text = "Mouse Leaves the TextBox";            
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt == true)
                label1.Text="The ALT has been pressed";
            else
                if (e.Control==true)
                    label1.Text="The Ctrl has been pressed";
                else
                    if (e.Shift==true)
                    label1.Text="The Shift has been pressed";                
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Alt == false || e.Control==false || e.Shift==false) 
                label1.Text = "The Key has been released";                    
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar)==true)
                  label1.Text = "You have pressed a Numeric key";
               else
                  if(char.IsLetter(e.KeyChar)==true)
                  label1.Text = "You have pressed a Letter key";
            
               
        }                
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(94, 23);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "Click Me";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(81, 78);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(103, 70);
            this.textBox1.TabIndex = 1;
            this.textBox1.MouseLeave += new System.EventHandler(this.textBox1_MouseLeave);
            this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
            this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp);
            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
            this.textBox1.MouseEnter += new System.EventHandler(this.textBox1_MouseEnter);
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(12, 169);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(268, 23);
            this.label1.TabIndex = 2;
            this.label1.Text = "label1";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // Events
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Events";
            this.Text = "Events";
            this.ResumeLayout(false);
            this.PerformLayout();

        }


        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Events());
        }
    }








23.64.event
23.64.1.Events
23.64.2.Using Event Accessors
23.64.3.Event and event handler
23.64.4.Use delegate to handle events
23.64.5.Fire event in property setter
23.64.6.Events: add and remove functions for a private delegate event
23.64.7.Events: add and remove functions
23.64.8.Events add and remove with synchronized block
23.64.9.Events: Custom Add and Remove with Global delegate cache.
23.64.10.A .NET-compatible event
23.64.11.Use the built-in EventHandler delegate
23.64.12.Use an anonymous method as an event handler
23.64.13.Ignored Parameters Anonymous Methods for Button click, kepressed and mouse clicked action
23.64.14.Events and form controls