Add an Event

Once you have opened the designer and added a button, it's time to add a click event to the button. This even will be triggered whenever the user clicks on the button.

There are two ways to add this:

  1. By double-clicking on the button

  2. By selecting the button, clicking on the event (lightning bolt) icon, and double-clicking on the click event

Either way, you will end up with the Page1.cs file open and the following code:

private void button1_Click(object sender, System.EventArgs e)
{

}

This is the function that is called whenever the button is clicked. We can add some code so that an Alertbox is displayed whenever the user clicks the button:

private void button1_Click(object sender, System.EventArgs e)
{
    AlertBox.Show("Button Clicked");
}

Run the program by clicking on this button at the top of Visual Studio:

Note that if you click on the dropdown menu, there are several options you can change:

You can change the web browser to Firefox, Google Chrome or Microsoft Edge

You can change the framework that the project is running on

Once the program is running, click the button. It will look like this:

Note that by default, the url will be localhost:5000 when running from Visual Studio

Last updated

Was this helpful?