Friday, November 8, 2024
HomeComputer programmingCreate an Exit Button in C# Visual Studio

Create an Exit Button in C# Visual Studio

Building an exit, close, or quit button is one of the first steps a programmer needs to learn when developing in C# Visual Studio. This article describes how to build the form and generate the code needed for this task. Screencast is included as well.

I’ll walk you through the steps with pictures. Plus, there is a video screencast at the end for a more detailed explanation.

What if you want someone to be able to exit your program by clicking a button? Using a simple button control and one line of code, you can make it happen.

1 Create a new button control on your C# form by dragging and dropping “button” control from the toolbox window to your form.

2 In your properties window, give the button a meaningful name. This will be the name that you will use as reference in the code.

Adjust the additional properties that your button control requires. To clarify, Text property is your front-facing button text, and Name is your code reference name for the button.

3 Your physical button should now reflect the name you gave it.

4 Double click the “Exit” button on the GUI and this will switch to code view and automatically create a class for your exit button.  As shown below, edit the click event for the button, and the code will execute when the button is clicked.

5 For our button to exit on click we need to enter the following code:

this.Close();

This code tells our program on the click event of this specific button, close THIS form.

6 Save your program and run it. Your program will exit when you click the exit button.

Chris Luongo
Chris Luongo
Chris is a self-taught web designer and developer out of Atlanta, GA. This geek enjoys coffee, cold brews, bike riding, and twisting any form of tech into submission.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!