Mouse event on a control : Mouse On Form « 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# / C Sharp » GUI Windows Form » Mouse On Form 




Mouse event on a control
Mouse event on a control

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

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

namespace Events
{
  /// <summary>
  /// Summary description for FormEvents.
  /// </summary>
  public class FormEvents : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button btnOK;
    private System.Windows.Forms.Button btnCancel;
    private System.Windows.Forms.TextBox textBox1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

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

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      // Add the following line to the constructor.
      Application.Idle += new System.EventHandler(OnIdle);
    }

    private void OnIdle (object sender, EventArgs e)
    {
      btnOK.Enabled = textBox1.Text.Length > 0;
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Disposebool disposing )
    {
      ifdisposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      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.btnOK = new System.Windows.Forms.Button();
      this.btnCancel = new System.Windows.Forms.Button();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      // 
      // btnOK
      // 
      this.btnOK.BackColor = System.Drawing.SystemColors.Control;
      this.btnOK.Location = new System.Drawing.Point(56192);
      this.btnOK.Name = "btnOK";
      this.btnOK.Size = new System.Drawing.Size(8024);
      this.btnOK.TabIndex = 0;
      this.btnOK.Text = "OK";
      this.btnOK.Click += new System.EventHandler(this.btnOK_OnClick);
      this.btnOK.MouseEnter += new System.EventHandler(this.OnMouseEnter);
      this.btnOK.MouseLeave += new System.EventHandler(this.OnMouseLeave);
      // 
      // btnCancel
      // 
      this.btnCancel.BackColor = System.Drawing.SystemColors.Control;
      this.btnCancel.Location = new System.Drawing.Point(160192);
      this.btnCancel.Name = "btnCancel";
      this.btnCancel.Size = new System.Drawing.Size(8824);
      this.btnCancel.TabIndex = 1;
      this.btnCancel.Text = "Cancel";
      this.btnCancel.Click += new System.EventHandler(this.btnCancel_OnClick);
      this.btnCancel.MouseEnter += new System.EventHandler(this.OnMouseEnter);
      this.btnCancel.MouseLeave += new System.EventHandler(this.OnMouseLeave);
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(48144);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(20020);
      this.textBox1.TabIndex = 2;
      this.textBox1.Text = "";
      // 
      // FormEvents
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(292273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.textBox1,
                                      this.btnCancel,
                                      this.btnOK});
      this.Name = "FormEvents";
      this.Text = "FormEvents";
      this.ResumeLayout(false);

    }
    #endregion

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

    private void btnOK_OnClick(object sender, System.EventArgs e)
    {
      MessageBox.Show ("OK button clicked""Events");
    }

    private void btnCancel_OnClick(object sender, System.EventArgs e)
    {
      MessageBox.Show ("Cancel button clicked""Events");
    }

    private void OnMouseEnter(object sender, System.EventArgs e)
    {
            // Test whether the sender is a button
      if (sender is Button)
      {
                // Change the OK button style to Flat, but the
                // change the background color of the Cancel button
        if (sender == btnOK)
          ((Buttonsender).FlatStyle = FlatStyle.Flat;
        else if (sender == btnCancel)
          ((Buttonsender).BackColor = SystemColors.ControlLightLight;
        return;
      }
            // Add code here to handle MouseEnter event for other controls
    }

    private void OnMouseLeave(object sender, System.EventArgs e)
    {
            // Test whether the sender is a button
      if (sender is Button)
      {
                // Change the OK button style to Flat, but the
                // change the background color of the Cancel button
        if (sender == btnOK)
          ((Buttonsender).FlatStyle = FlatStyle.Standard;
        else if (sender == btnCancel)
          ((Buttonsender).BackColor = SystemColors.Control;
        return;
      }
            // Add code here to handle MouseLeave event for other controls
    }
  }
}


           
       














Related examples in the same category
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.