Add action handler to button : Button « GUI Windows Forms « C# / CSharp Tutorial






Add action handler to button
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class ButtonActionForm : System.Windows.Forms.Form
{
  private System.Windows.Forms.Button button1;
  private System.ComponentModel.Container components = null;

  public ButtonActionForm()
  {
    InitializeComponent();

  }

  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  private void InitializeComponent()
  {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();

    this.button1.Location = new System.Drawing.Point(109, 122);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "Say Hello";
    this.button1.Click += new System.EventHandler(this.button1_Click);

    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.button1});
    this.Name = "ButtonActionForm";
    this.ResumeLayout(false);

  }

  [STAThread]
  static void Main() 
  {
    Application.Run(new ButtonActionForm());
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show( this, "Hello" );
  }
}








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