How to Use Form Load and Button click Event in C#
By FoxLearn 12/10/2024 2:37:13 AM 27.23K
Below is an example that demonstrates how to create a simple Windows Forms application with a Form Load event and a Button Click event.
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; 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 update UI from another thread in C#
- How to get CheckedListBox selected values in C#
- How to use Advanced Filter DataGridView in C#
- How to create a Notification Popup in C#
- How to Link Chart /Graph with Database in C#
- How to Check SQL Server Connection in C#
- How to Generate Serial Key in C#
- How to Search DataGridView by using TextBox in C#