How to Use Form Load and Button click Event in C#
By FoxLearn 2/19/2025 2:34:41 AM 28.25K
Below is an example that demonstrates how to create a simple Windows Forms application with a Form Load event and a Button Click event.
How to call button click event in c# windows forms?
Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "FormLoadAndButtonClick" and then click OK
In the designer view, Drag and drop the Label, Button controls from the Toolbox onto the form, you can design your form as shown below.
Double-click on the form to create the Form1_Load
event handler.
Double-click on the button to create the btnClear_Click
, btnExit_Click
, btnClickMe_Click
event handler.
Add code to handle your form as below
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; // load c# form event namespace FormLoadAndButtonClick { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // c# load form event private void Form1_Load(object sender, EventArgs e) { //Set text to lblFormLoad label on Form_Load event lblFormLoad.Text = "This text is set on startup !"; } // c# button click event private void btnClickMe_Click(object sender, EventArgs e) { //Set text to lblButtonClick on button click event lblButtonCLick.Text = "This text is set on button click !"; } // c# button click event private void btnClear_Click(object sender, EventArgs e) { lblButtonCLick.Text = string.Empty; } // c# button click event private void btnExit_Click(object sender, EventArgs e) { //Exit your application Application.Exit(); } } }
VIDEO TUTORIAL
- How to use Error Provider in C#
- How to use Advanced Filter DataGridView in C#
- How to zoom an image in C#
- How to Send and Receive email in Microsoft Outlook using C#
- How to use Context Menu Strip in C#
- How to update UI from another thread in C#
- How to Convert text to speech in C#
- How to Open and Show a PDF file in C#