Learn Buttons in C#
Windows Forms
via simple examples
This tutorial will help you learn the following concepts:
- How to create a
Windows Form
with a Button programmatically. - How to set Button properties like
Text
andLocation
programmatically. - How to handle button clicks in a button programmatically.
Example 1: Simple Button Example
Learn button in C# via these simple step by step exammples.
This example will comprise the following files:
Button.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). Button.cs
Create a file named Button.cs
Add using
statements:
using System;
using System.Windows.Forms;
Create namespace:
namespace Examples {
Inherit from the Form
class:
class Form1 : Form {
Instantiate our Buttons as insance fields of this class
:
private Button button1 = new Button();
private Button button2 = new Button();
private Label label1 = new Label();
private Label label2 = new Label();
private int button1Clicked = 0;
private int button2Clicked = 0;
Create our Main()
method:
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
Declare the constructor
:
public Form1() {
Set the current form as the parent of our Button
button1.Parent = this;
Set the Text
property of our Button
button1.Text = "button1";
Set the location of our button:
button1.Location = new System.Drawing.Point(50, 50);
Handle click events of the button and subsequently update our label:
button1.Click += delegate (object dender, EventArgs e) {
label1.Text = string.Format("button1 clicked {0} times", ++button1Clicked);
};
Create another button
button2.Parent = this;
button2.Text = "button2";
button2.Location = new System.Drawing.Point(50, 100);
button2.Size = new System.Drawing.Size(200, 75);
button2.Click += delegate (object sender, EventArgs e) {
label2.Text = string.Format("button2 clicked {0} times", ++button2Clicked);
};
Here is the full code
using System;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
private Button button1 = new Button();
private Button button2 = new Button();
private Label label1 = new Label();
private Label label2 = new Label();
private int button1Clicked = 0;
private int button2Clicked = 0;
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1() {
button1.Parent = this;
button1.Text = "button1";
button1.Location = new System.Drawing.Point(50, 50);
button1.Click += delegate (object dender, EventArgs e) {
label1.Text = string.Format("button1 clicked {0} times", ++button1Clicked);
};
button2.Parent = this;
button2.Text = "button2";
button2.Location = new System.Drawing.Point(50, 100);
button2.Size = new System.Drawing.Size(200, 75);
button2.Click += delegate (object sender, EventArgs e) {
label2.Text = string.Format("button2 clicked {0} times", ++button2Clicked);
};
label1.Parent = this;
label1.Text = "button1 clicked 0 times";
label1.Location = new System.Drawing.Point(50, 200);
label1.Width = 200;
label2.Parent = this;
label2.Text = "button2 clicked 0 times";
label2.Location = new System.Drawing.Point(50, 230);
label2.Width = 200;
}
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
Example 2: C# Button Example
Simple step by step Button example in C#.
This tutorial will help you learn the following concepts:
- How to create buttons programmatically in C# WinForms.
- How to handle Button clicks using
delegate
This example will comprise the following files:
Button2.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project
Step 2: Write Code
Write Code as follows:
*(a). Button2.cs
Create a file named Button2.cs
Add the following namespace
namespace Examples {
Extend the System.Windows.Forms.Form
:
class Form1 : System.Windows.Forms.Form {
Define our Form1
constructor
```csharp
public Form1() {
Onto the Controls
property of our Form
, instantiate our System.Windows.Forms.Button
objects, set their properties and add them
this.Controls.Add(new System.Windows.Forms.Button { Text = "button1", Location = new System.Drawing.Point(50, 50) });
this.Controls.Add(new System.Windows.Forms.Button { Text = "button2", Location = new System.Drawing.Point(50, 100), Size = new System.Drawing.Size(200, 75) });
this.Controls.Add(new System.Windows.Forms.Label { Text = "button1 clicked 0 times", Location = new System.Drawing.Point(50, 200), Size = new System.Drawing.Size(200, 20) });
this.Controls.Add(new System.Windows.Forms.Label { Text = "button2 clicked 0 times", Location = new System.Drawing.Point(50, 230), Size = new System.Drawing.Size(200, 20) });
this.Controls[0].Click += (sender, e) => this.Controls[2].Text = string.Format("button1 clicked {0} times", ++this.button1Clicked);
this.Controls[1].Click += (sender, e) => this.Controls[3].Text = string.Format("button2 clicked {0} times", ++this.button2Clicked);
}
Here is the full code
namespace Examples {
//end
class Form1 : System.Windows.Forms.Form {
//end
//Create the main method
and inside it EnableVisualStyles
and run the application:
public static void Main() {
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.Run(new Form1());
}
//end
public Form1() {
//end
this.Controls.Add(new System.Windows.Forms.Button { Text = "button1", Location = new System.Drawing.Point(50, 50) });
this.Controls.Add(new System.Windows.Forms.Button { Text = "button2", Location = new System.Drawing.Point(50, 100), Size = new System.Drawing.Size(200, 75) });
this.Controls.Add(new System.Windows.Forms.Label { Text = "button1 clicked 0 times", Location = new System.Drawing.Point(50, 200), Size = new System.Drawing.Size(200, 20) });
this.Controls.Add(new System.Windows.Forms.Label { Text = "button2 clicked 0 times", Location = new System.Drawing.Point(50, 230), Size = new System.Drawing.Size(200, 20) });
this.Controls[0].Click += (sender, e) => this.Controls[2].Text = string.Format("button1 clicked {0} times", ++this.button1Clicked);
this.Controls[1].Click += (sender, e) => this.Controls[3].Text = string.Format("button2 clicked {0} times", ++this.button2Clicked);
}
private int button1Clicked = 0, button2Clicked = 0;
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |