Csharp/C Sharp/GUI Windows Form/Mouse On Form — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

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.ruponentModel;
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.ruponentModel.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 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.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(56, 192);
      this.btnOK.Name = "btnOK";
      this.btnOK.Size = new System.Drawing.Size(80, 24);
      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(160, 192);
      this.btnCancel.Name = "btnCancel";
      this.btnCancel.Size = new System.Drawing.Size(88, 24);
      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(48, 144);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(200, 20);
      this.textBox1.TabIndex = 2;
      this.textBox1.Text = "";
      // 
      // FormEvents
      // 
      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.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)
          ((Button) sender).FlatStyle = FlatStyle.Flat;
        else if (sender == btnCancel)
          ((Button) sender).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)
          ((Button) sender).FlatStyle = FlatStyle.Standard;
        else if (sender == btnCancel)
          ((Button) sender).BackColor = SystemColors.Control;
        return;
      }
            // Add code here to handle MouseLeave event for other controls
    }
  }
}