Add mouse click event to a Label

image_pdfimage_print


   

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
{
   Label Label1;
   TextBox TextBox1;
   Button Button1;

  public Form1()
  {
        InitializeComponent();
  }
   private void ctrlClick(System.Object sender, EventArgs e)
   {
     Control ctrl = (Control)sender;
     MessageBox.Show("You clicked: " + ctrl.Name);
   }

  private void InitializeComponent()
  {
    this.Label1 = new System.Windows.Forms.Label();
    this.TextBox1 = new System.Windows.Forms.TextBox();
    this.Button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // Label1
    // 
    this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.Label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Label1.Location = new System.Drawing.Point(14, 97);
    this.Label1.Name = "Label1";
    this.Label1.Size = new System.Drawing.Size(112, 24);
    this.Label1.TabIndex = 8;
    this.Label1.Text = "Label1";
    this.Label1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // TextBox1
    // 
    this.TextBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.TextBox1.Location = new System.Drawing.Point(12, 61);
    this.TextBox1.Name = "TextBox1";
    this.TextBox1.Size = new System.Drawing.Size(156, 21);
    this.TextBox1.TabIndex = 7;
    this.TextBox1.Text = "TextBox1";
    this.TextBox1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // Button1
    // 
    this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.Button1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Button1.Location = new System.Drawing.Point(12, 21);
    this.Button1.Name = "Button1";
    this.Button1.Size = new System.Drawing.Size(96, 28);
    this.Button1.TabIndex = 6;
    this.Button1.Text = "Button1";
    this.Button1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(367, 281);
    this.Controls.Add(this.Label1);
    this.Controls.Add(this.TextBox1);
    this.Controls.Add(this.Button1);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Control Medley";
    this.ResumeLayout(false);
    this.PerformLayout();

  }


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

}



           
          


Add mouse click event to TextBox

image_pdfimage_print
   

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
{
   Label Label1;
   TextBox TextBox1;
   Button Button1;

  public Form1()
  {
        InitializeComponent();
  }
   private void ctrlClick(System.Object sender, EventArgs e)
   {
     Control ctrl = (Control)sender;
     MessageBox.Show("You clicked: " + ctrl.Name);
   }

  private void InitializeComponent()
  {
    this.Label1 = new System.Windows.Forms.Label();
    this.TextBox1 = new System.Windows.Forms.TextBox();
    this.Button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // Label1
    // 
    this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.Label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Label1.Location = new System.Drawing.Point(14, 97);
    this.Label1.Name = "Label1";
    this.Label1.Size = new System.Drawing.Size(112, 24);
    this.Label1.TabIndex = 8;
    this.Label1.Text = "Label1";
    this.Label1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // TextBox1
    // 
    this.TextBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.TextBox1.Location = new System.Drawing.Point(12, 61);
    this.TextBox1.Name = "TextBox1";
    this.TextBox1.Size = new System.Drawing.Size(156, 21);
    this.TextBox1.TabIndex = 7;
    this.TextBox1.Text = "TextBox1";
    this.TextBox1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // Button1
    // 
    this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.Button1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Button1.Location = new System.Drawing.Point(12, 21);
    this.Button1.Name = "Button1";
    this.Button1.Size = new System.Drawing.Size(96, 28);
    this.Button1.TabIndex = 6;
    this.Button1.Text = "Button1";
    this.Button1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(367, 281);
    this.Controls.Add(this.Label1);
    this.Controls.Add(this.TextBox1);
    this.Controls.Add(this.Button1);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Control Medley";
    this.ResumeLayout(false);
    this.PerformLayout();

  }


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

}



           
          


System Events: User Preference Changed

image_pdfimage_print


   


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

public class Form1 : Form
{
    private System.Windows.Forms.Label label2;
    
  public Form1() {
        this.Font = SystemFonts.IconTitleFont;
        this.AutoScaleDimensions = new System.Drawing.SizeF(6.0F, 13.0F);
        InitializeComponent();
        SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
  }

    private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
    {
        if (e.Category == UserPreferenceCategory.Window)
        {
            this.Font = SystemFonts.IconTitleFont;
        }
    }

    private void InitializeComponent()
    {
        this.label2 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label2
        // 
        this.label2.Location = new System.Drawing.Point(12, 9);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(220, 55);
        this.label2.TabIndex = 2;
        this.label2.Text = "Try changing the Small Fonts/Large Fonts setting for th" +
            "e computer.";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(244, 138);
        this.Controls.Add(this.label2);
        this.Text = "Form1";
        this.ResumeLayout(false);

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

}


           
          


Get Event sender in event handler

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class TwoButtons: Form
{
     readonly Button btnLarger, btnSmaller;
     readonly int    cxBtn, cyBtn, dxBtn;
   
     public static void Main()
     {
          Application.Run(new TwoButtons());
     }
     public TwoButtons()
     {
          ResizeRedraw = true;
   
          cxBtn = 200;
          cyBtn = 100;
          dxBtn = 12;
   
          btnLarger = new Button();
          btnLarger.Parent = this;
          btnLarger.Text   = "&Larger";
          btnLarger.Size   = new Size(cxBtn, cyBtn);
          btnLarger.Click += new EventHandler(ButtonOnClick);
   
          btnSmaller = new Button();
          btnSmaller.Parent = this;
          btnSmaller.Text   = "&Smaller";
          btnSmaller.Size   = new Size(cxBtn, cyBtn);
          btnSmaller.Click += new EventHandler(ButtonOnClick);
   
          OnResize(EventArgs.Empty);
     }
     protected override void OnResize(EventArgs ea)
     {
          base.OnResize(ea);
   
          btnLarger.Location =
                         new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2,
                                  (ClientSize.Height - cyBtn) / 2);
          btnSmaller.Location =
                         new Point(ClientSize.Width / 2 + dxBtn / 2,
                                  (ClientSize.Height - cyBtn) / 2);
     }
     void ButtonOnClick(object obj, EventArgs ea)
     {
          Button btn = (Button) obj;
   
          if (btn == btnLarger)
          {
               Left   -= (int)(0.1 * Width);
               Top    -= (int)(0.1 * Height);
               Width  += (int)(0.1 * Width);
               Height += (int)(0.1 * Height);
          }
          else
          {
               Left   += (int)(Width  / 22f);
               Top    += (int)(Height / 22f);
               Width  -= (int)(Width  / 11f);
               Height -= (int)(Height / 11f);
          }
     }
}

    


Hit Testing

image_pdfimage_print


   

/*
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.Drawing.Drawing2D;

namespace HitTesting
{
    /// <summary>
    /// Summary description for HitTesting.
    /// </summary>
    public class HitTesting : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        private System.Windows.Forms.Label label1;
        
        GraphicsPath gP;
        string mes = "Move to the big I!";
        FontFamily fF = new FontFamily("Times new roman");

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

            CreateFont();
            this.Text = "Hit Testing";
            this.label1.Font = new Font(fF, 12);
            //
            // 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.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(88, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(160, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            // 
            // HitTesting
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 109);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.label1});
            this.Name = "HitTesting";
            this.Text = "HitTesting";
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.HitTesting_MouseMove);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new HitTesting());
        }
        void CreateFont()
        {
            Graphics g = this.CreateGraphics();
            label1.Text = mes;
            string s = "I";
            int fSt = (int)FontStyle.Regular;
            Point xy = new Point(50, 10);
            StringFormat sFr = StringFormat.GenericDefault;
 
            gP = new GraphicsPath();  // gp is a class member
            gP.AddString(s, fF, fSt, 50, xy, sFr);  // add the string to the path
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.DrawPath(Pens.Black, gP);  // draw the path to the surface
            g.Dispose();
        }

        private void HitTesting_MouseMove(object sender, MouseEventArgs e)
        {
            Region reg = new Region(gP);
            if(reg.IsVisible(new Point(e.X, e.Y)))
                mes = "You touched me ...";
            else 
                mes = "Move to the big I!";
            CreateFont();
        }
    }
}

           
          


Mouse Handler

image_pdfimage_print


   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/

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

namespace MouseHandler_c
{
    /// <summary>
    /// Summary description for Finally_c.
    /// </summary>
    public class MouseHandler_c : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button Off;
        private System.Windows.Forms.Button On;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public MouseHandler_c()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            this.MouseMove += new 
                     System.Windows.Forms.MouseEventHandler(this.MyMouseHandler);

        }

        /// <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.Off = new System.Windows.Forms.Button();
              this.On = new System.Windows.Forms.Button();
              this.SuspendLayout();
              // 
              // label1
              // 
              this.label1.Location = new System.Drawing.Point(40, 56);
              this.label1.Name = "label1";
              this.label1.Size = new System.Drawing.Size(176, 16);
              this.label1.TabIndex = 0;
              this.label1.Text = "label1";
              // 
              // Off
              // 
              this.Off.Location = new System.Drawing.Point(40, 216);
              this.Off.Name = "Off";
              this.Off.Size = new System.Drawing.Size(72, 24);
              this.Off.TabIndex = 1;
              this.Off.Text = "Off";
              this.Off.Click += new System.EventHandler(this.Off_Click);
              // 
              // On
              // 
              this.On.Location = new System.Drawing.Point(152, 216);
              this.On.Name = "On";
              this.On.Size = new System.Drawing.Size(72, 24);
              this.On.TabIndex = 2;
              this.On.Text = "On";
              this.On.Click += new System.EventHandler(this.On_Click);
              // 
              // MouseHandler_c
              // 
              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
              this.ClientSize = new System.Drawing.Size(292, 273);
              this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.On,
                                                                          this.Off,
                                                                          this.label1});
              this.Name = "MouseHandler_c";
              this.Text = "MouseHandler_c";
              this.Load += new System.EventHandler(this.MouseHandler_c_Load);
              this.ResumeLayout(false);

        }
        #endregion

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

        private void MouseHandler_c_Load(object sender, System.EventArgs e)
        {
        }
        private void MyMouseHandler(object sender, 
                                    System.Windows.Forms.MouseEventArgs e)
        {
          label1.Text = "X= " + e.X.ToString() + ", Y= " + e.Y.ToString();
        }
    
        protected override void OnMouseMove( MouseEventArgs e )
        {
          base.OnMouseMove(e);
        }
    
        private void Off_Click(object sender, System.EventArgs e)
        {
          this.MouseMove -= new 
            System.Windows.Forms.MouseEventHandler(this.MyMouseHandler);
        }
    
        private void On_Click(object sender, System.EventArgs e)
        {
          this.MouseMove += new 
            System.Windows.Forms.MouseEventHandler(this.MyMouseHandler);
        }

    }
}


           
          


Full screen and KeyEvent and MouseEvent

image_pdfimage_print
   

// This Article is a simple one to show the usage of KeyEvent and MouseEvent. 
  
// Please move the mouse or press a key to Exit this Program. 
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 

namespace ScreenSave 
{ 
    public class KeyEventandMouseEvent : System.Windows.Forms.Form { 
        private System.Windows.Forms.Label label1; 
        private System.Windows.Forms.Timer timer1; 
        private System.ComponentModel.IContainer components; 
        private Boolean boolFlag; 
        private int ixStart; 
        private int iyStart; 
    
        public KeyEventandMouseEvent() { 
            // 
            // Required for Windows Form Designer support 
            // 
            InitializeComponent(); 
        } 
        /// <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.components = new System.ComponentModel.Container(); 
            this.timer1 = new System.Windows.Forms.Timer(this.components); 
            this.label1 = new System.Windows.Forms.Label(); 
            this.SuspendLayout(); 
            // 
            // timer1 
            // 
            this.timer1.Enabled = true; 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 
            // 
            // label1 
            // 
            this.label1.Font = new System.Drawing.Font("Times New Roman", 14F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 
            this.label1.ForeColor = System.Drawing.Color.Lime; 
            this.label1.Location = new System.Drawing.Point(8, 280); 
            this.label1.Name = "label1"; 
            this.label1.Size = new System.Drawing.Size(312, 32); 
            this.label1.TabIndex = 0; 
            this.label1.Text = "Roller - Developed in C-Sharp"; 
            this.label1.Click += new System.EventHandler(this.label1_Click); 
            // 
            // KeyEventandMouseEvent 
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
            this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(64)), ((System.Byte)(64))); 
            this.ClientSize = new System.Drawing.Size(568, 32); 
            this.Controls.AddRange(new System.Windows.Forms.Control[] { 
            this.label1}); 
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 
            this.KeyPreview = true; 
            this.Name = "KeyEventandMouseEvent"; 
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
            this.Text = "KeyEventandMouseEvent"; 
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized; 
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.OnKeyPressEvent); 
            this.Load += new System.EventHandler(this.KeyEventandMouseEvent_Load); 
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMoveEvent); 
            this.ResumeLayout(false); 
        } 
        #endregion 
        /// <summary> 
        /// The main entry point for the application. 
        /// </summary> 
        [STAThread] 
        static void Main() 
        { 
            Application.Run(new KeyEventandMouseEvent()); 
        } 
        private void timer1_Tick(object sender, System.EventArgs e) 
        { 
            int i; 
              
            i = label1.Location.X; 
            if (i >= 750) 
            i = 0; 
            else 
            i = label1.Location.X + 5; 
              
            label1.Location = new Point(i,280); 
        } 
        private void KeyEventandMouseEvent_Load(object sender, System.EventArgs e) 
        { 
          
        } 
        private void OnMouseMoveEvent(object sender,MouseEventArgs e) 
        { 
            //Application.Exit(); 
            if (ixStart == 0 &amp;&amp; iyStart == 0) 
            { 
                ixStart = e.X; 
                iyStart = e.Y; 
            } 
            else if (e.X != ixStart || e.Y != iyStart) 
                Application.Exit(); 
              
        } 
        private void OnKeyPressEvent(object sender, System.Windows.Forms.KeyPressEventArgs e) 
        { 
            Application.Exit(); 
        } 
        private void label1_Click(object sender, System.EventArgs e) 
        { 
        } 
    } 
}