Button mouse event : Button « GUI Windows Forms « C# / CSharp Tutorial






Button mouse event
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_MouseEnter(object sender, EventArgs e)
    {
        button1.Text = "The mouse is over me";
    }

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        button1.Text = "The mouse is not over me";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Button clickedButton = sender as Button;
        if (clickedButton != null)
        {
            clickedButton.Text = "I was clicked";
        }
    }
}
partial class Form1
{
    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(86, 89);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(172, 86);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.MouseLeave += new System.EventHandler(this.button1_MouseLeave);
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.button1.MouseEnter += new System.EventHandler(this.button1_MouseEnter);
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(69, 25);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(75, 23);
        this.button2.TabIndex = 1;
        this.button2.Text = "button2";
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
}
public class ButtonMouseEvent
{
    [STAThread]
    static void Main()
    {
        Application.Run(new Form1());
    }
}








23.8.Button
23.8.1.Add a Button
23.8.2.Handle button messages
23.8.3.Relocate button after pressing button
23.8.4.Add Image in an ImageList to a ButtonAdd Image in an ImageList to a Button
23.8.5.Use delegate to add an event handler to a ButtonUse delegate to add an event handler to a Button
23.8.6.Button mouse eventButton mouse event
23.8.7.Set Label text in a button click eventSet Label text in a button click event
23.8.8.Add action handler to buttonAdd action handler to button
23.8.9.Button Style: image button, standard buttonButton Style: image button, standard button
23.8.10.Button PerformClick MethodButton PerformClick Method
23.8.11.Button Flat StyleButton Flat Style
23.8.12.Calculate Button size based on its TextCalculate Button size based on its Text
23.8.13.Button Background ColorButton Background Color
23.8.14.Button Text AlignmentButton Text Alignment
23.8.15.Button ImageButton Image
23.8.16.Auto Scale Button
23.8.17.extends Button
23.8.18.Device Independent Button