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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » Mouse On FormScreenshots 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.