Introduction
For example, let's see how to add a button to a form.- Open the Toolbox:
- Double-click Button to add it to the form
- Change the Text property of the button
- Change the Name property
- Save your project as:
Add functionality to the button
If you want to display a message when the button is clicked, a piece of code must be added to the button.
Double-click the button to display the code window. It shows the automatically generated code for the whole form application.
The following code will create a pop up window and display a message:
MessageBox.Show("Thanks for clicking the button!");
The completed code should look like
Click Save All.
Let's test the program by clicking Start to run the program!
Congratulations! Here is how it looks:
Create another button
Repeat the steps to add an exit button to the form. Use the following Button properties:
- Text: Exit the form
- Name: exitButton
- Code for the button: this.Close();
- Save All
- Run the program to see