Buttons in C# WPF(Windows platform) and WFA(Web Form Applications)
Buttons are an essential part of any application whether it is desktop or web based. Now the Question is how to create buttons in C# and how to use them and perform some events on buttons click events.
Create a button in C#
First thing is that by using Visual Studio you have to simply drag and drop buttons from the toolbox. And visual studio also provides a wide range of button properties in GUI mode so that it less your programming efforts and it’s easy to put buttons of different styles and size on web pages and windows forms.
So First to put a button on design area we have to drag it from toolbox if tool box on the visual studio is not activated then click on View or press alt+I and select tool box from it .(It’s same for both platforms).

Button Properties
The built in provided properties is shown on the right side of the window. if they are not shown right click on a button and select properties:

You can change any property of button interface from here like text, text color, font size background color, text-align etc.
Now if we want to perform some action when user click on the button so to code this simply double click on the button and the C# code file is open with .cs extension and there is a ready-made function same the name you put for the button like:
1 2 3 |
private void button1_Click(object sender, EventArgs e) { } |
Now code here what we want when the user clicks on the button. This time, I want when user click button a message box pop up and say hello world.
Now C# code is :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("hello World"); } } } |
this click button handler can also be generated by double click on the button in the visual panel. I just placed a message box int the button handler i.e.
1 |
MessageBox.Show("hello World"); |
Running Button
If we run the code and click on the button it will show a popup dialog box.

if I want to change the text, color, and width of button using c# then the code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { button1.Text = "Click Me"; button1.ForeColor = Color.Chocolate; button1.BackColor = Color.Aqua; button1.Width =200; } } } |

So as the above we change the properties of the button using the console or also by using C# code.
Button in ASP.NET
Now take a loop on buttons in ASP.net Web pages (.aspx). In Web Forms we have some more advantages one is we manually create the buttons using asp button format which is:
1 |
<asp:Button ID="Button1" runat="server" Text="Button" /> |
Or simply drag it from the toolbox. Other is we apply styles on it using CSS. And the code for all changes is same as in windows palette form.