Mouse event handlers : Mouse Event « GUI Windows Forms « C# / CSharp Tutorial






Mouse event handlers
using System;
using System.Drawing;
using System.Windows.Forms;

public class MouseEvents : Form
{
  private Label lbl;

  public MouseEvents()
  {
    Size = new Size(400,600);

    lbl = new Label();
    lbl.Parent = this;
    lbl.Location = new Point(50,50);
    lbl.Size = new Size(150,25);
    lbl.BorderStyle = BorderStyle.Fixed3D;
    lbl.MouseEnter += new System.EventHandler(lbl_MouseEnter);
    lbl.MouseHover += new System.EventHandler(lbl_MouseHover);
    lbl.MouseLeave += new System.EventHandler(lbl_MouseLeave);
    lbl.MouseDown += new System.Windows.Forms.MouseEventHandler(lbl_MouseDown);
    lbl.MouseMove += new System.Windows.Forms.MouseEventHandler(lbl_MouseMove);
    lbl.MouseUp += new System.Windows.Forms.MouseEventHandler(lbl_MouseUp);
    lbl.MouseWheel += new System.Windows.Forms.MouseEventHandler(lbl_MouseWheel);
    lbl.Click += new System.EventHandler(lbl_Click);
    lbl.DoubleClick += new System.EventHandler(lbl_DoubleClick);

  }

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

  private void btnReset_Click(object sender, EventArgs e)
  {
    lbl.Text = "";
  }

  private void lbl_MouseEnter(object sender, EventArgs e)
  {
    lbl.Text = "MouseEnter";
    TextBoxDraw("Label MouseEnter");
  }

  private void lbl_MouseHover(object sender, EventArgs e)
  {
    lbl.Text = "MouseHover";
    TextBoxDraw("Label MouseHover");
  }

  private void lbl_MouseLeave(object sender, EventArgs e)
  {
    lbl.Text = "MouseLeave";
    TextBoxDraw("Label MouseLeave");
  }

  private void lbl_MouseDown(object sender, MouseEventArgs e)
  {
    lbl.Text = "MouseDown";
    string str;
    str = "Label MouseDown";
    str += "\r\n\tButton:  " + e.Button.ToString();
    str += "\r\n\tClicks:  " + e.Clicks.ToString();
    str += "\r\n\tDelta:  " + e.Delta.ToString();
    str += "\r\n\tX:  " + e.X.ToString();
    str += "\r\n\tY:  " + e.Y.ToString();
    TextBoxDraw(str);
  }

  private void lbl_MouseMove(object sender, MouseEventArgs e)
  {
    lbl.Text = "MouseMove";
    string str;
    str = "Label MouseMove";
    str += "\r\n\tButton:  " + e.Button.ToString();
    str += "\r\n\tClicks:  " + e.Clicks.ToString();
    str += "\r\n\tDelta:  " + e.Delta.ToString();
    str += "\r\n\tX:  " + e.X.ToString();
    str += "\r\n\tY:  " + e.Y.ToString();
    TextBoxDraw(str);
  }

  private void lbl_MouseUp(object sender, MouseEventArgs e)
  {
    lbl.Text = "MouseUp";
    string str;
    str = "Label MouseUp";
    str += "\r\n\tButton:  " + e.Button.ToString();
    str += "\r\n\tClicks:  " + e.Clicks.ToString();
    str += "\r\n\tDelta:  " + e.Delta.ToString();
    str += "\r\n\tX:  " + e.X.ToString();
    str += "\r\n\tY:  " + e.Y.ToString();
    TextBoxDraw(str);
  }

  private void lbl_MouseWheel(object sender, MouseEventArgs e)
  {
    lbl.Text = "MouseWheel";
    string str;
    str = "Label MouseWheel";
    str += "\r\n\tButton:  " + e.Button.ToString();
    str += "\r\n\tClicks:  " + e.Clicks.ToString();
    str += "\r\n\tDelta:  " + e.Delta.ToString();
    str += "\r\n\tX:  " + e.X.ToString();
    str += "\r\n\tY:  " + e.Y.ToString();
    TextBoxDraw(str);
  }

  private void lbl_Click(object sender, EventArgs e)
  {
    lbl.Text = "Click";
    TextBoxDraw("Label Click");
  }

  private void lbl_DoubleClick(object sender, EventArgs e)
  {
    lbl.Text = "DoubleClick";
    TextBoxDraw("Label DoubleClick");
  }

  protected override void OnMouseEnter(EventArgs e)
  {
    base.OnMouseEnter(e);
    TextBoxDraw("Form MouseEnter");
  }

  protected override void OnMouseHover(EventArgs e)
  {
    base.OnMouseHover(e);
    TextBoxDraw("Form MouseHover");
  }

  protected override void OnMouseLeave(EventArgs e)
  {
    base.OnMouseLeave(e);
    TextBoxDraw("Form MouseLeave");
  }
  private void TextBoxDraw(string str){
      Console.WriteLine(str);
  }
}








23.62.Mouse Event
23.62.1.Mouse Event information
23.62.2.Mouse PropertiesMouse Properties
23.62.3.Mouse event handlersMouse event handlers
23.62.4.Mouse Enter / Hover / LeaveMouse Enter / Hover / Leave
23.62.5.Mouse Button Left clicked