C Sharp Hello World Tutorial

Write a simple Windows desktop application using Visual Studio 2017 in this C Sharp hello world tutorial. Visual C# 2017 is used to create a window with two labels and a button. When the button is clicked a “hello world” message is displayed in one of the labels. The other label shows a count value that is incremented with each button click.

The image below shows the finished C# application that is built in this tutorial.

C Sharp Hello World Tutorial Application
C Sharp Hello World Tutorial Application

Before continuing with this C Sharp hello world tutorial, install the free version of Visual Studio called Visual Studio Community.

C Sharp Hello World Tutorial Steps

The following tutorial steps show how to build the C# hello world application.

1. Start Visual Studio and Create a New C# Application

Open Visual Studio by clicking the Windows start menu icon and searching for Visual Studio 2017.

1.1 Create a New C# Windows Forms App

Start a new C# Visual Studio project by selecting File ⇒ New ⇒ Project… from the top menu. In the New Project dialog box that pops up, select Visual C# in the left pane and Windows Forms App (.NET Framework) in the middle pane. Give the project a name, such as hello_world, in the Name field of the dialog box. Select the destination folder to save the project in using the Browse… button.

Visual Studio 2017 New C Sharp Project
Visual Studio 2017 New C Sharp Project

Make sure that Create directory for solution is checked as shown in the above image. Click the OK button to create the new project.

1.2 Rename the Default Form File Name

A new Windows Forms App project contains a default form called Form1 with a default file name of Form1.cs. To change the name of this form, first click Form1.cs in the top right Solution Explorer pane in Visual Studio to select it. Now either press the F2 keyboard key, or click the Form1.cs text (not the icon) to make it editable. Change the form name to FormMain.cs. When a dialog box pops up asking if you would like to rename references to Form1, click Yes.

1.3 Rename the Form Title

Renaming the form title text changes the text in the top title bar of the form and of the final application window. To change the form title, first make sure that the main form is selected. Now find the Text field in the lower right Properties pane of Visual Studio. The default text is Form1. Change it to Hello World. Hit the Enter key after typing the new title to apply the change. The form should now look as follows.

C Sharp Hello World Tutorial Main Form
C Sharp Hello World Tutorial Main Form

Click the Save All icon on the top toolbar to save the changes to the project.

2. Place the Application Controls

Place two labels and a button on the main form. Both Label and Button controls are found under Common Controls in the left Toolbox pane in Visual Studio. To place a control, click it in the left pane and then click in the main form at the position that you would like to place it. After placing the controls your form should look as follows.

Label and Button Form Controls Placed
Label and Button Form Controls Placed

3. Edit the Windows Form Controls

Edit the Windows form controls for the application that were placed in the previous tutorial step to give them meaningful names and text.

3.1 Edit the Top Label

Click the top label to select it. In the lower right Properties pane of Visual Studio, find the Text field and change its value to 0. This label is used to display an incrementing count and so must start with an initial value of zero.

Scroll up the Properties pane for the top label to find the label name which has a default name of label1 next to the (Name) field. Change the label name to LabelCount. This gives the label a meaningful name in the C# code for this project.

3.2 Edit the Second Label

Click the second label to select it. As with the previous label change its text and name to the following:

Text: …

(Name): LabelHello

This label is used to display the “hello world” message. Its text is changed to … as a place holder that will be replaced by the message when the button is clicked.

3.3 Edit the Button

Click the button to select it. Change the button Text to &Hello. Change the button name to ButtonHello.

Placing an ampersand (&) before the Hello text of the button underscores the first letter of (H) of the button text in the button control on the form. This also allows the button to be clicked by using Alt + H on the keyboard when the application is running and in focus.

3.4 Position the Controls

Line the controls up neatly by dragging them into position. Drag the button so that it is in the middle of the form horizontally, but near the bottom of the form. Resize the height of the form by clicking the form and dragging the bottom handle up.

When the labels are dragged a line will appear at their left as they line up with the left side of the button. Use this line to align both labels to the left of the button.

Your form window should now look something likethe image below.

Finished Hello World C Sharp Form
Finished Hello World C Sharp Form

3. Write the C# Code

Double click the Hello button on the form to go to the function that will execute when the button is clicked in the application.

3.1 Add a Count Variable

A variable is needed to hold the count value for this application. Add an integer variable called count to the form object as shown below. This is done in FormMain.cs.

3.2 Add the Button Click Event Code

After double-clicking the button on the main form, a function was added to the code called ButtonHello_Click(). This code is run every time the application receives a button click event. In other words the code will run each time a user of the application clicks the button.

Edit ButtonHello_Click() to add the following operational code to the application.

At the top of the function, the variable count is incremented. The variable is then converted to a string which is used to update the count label text.

The string “Hello, world!” is used to update the hello label in the last line of code in the function.

3.3 Full C# Code Listing

The full listing for the code in FormMain.cs is shown below.

Make the above changes and save the project. Click the Start button on the top Visual Studio toolbar to build and run the application.

4. Finishing Touches

After building and running the application, you may want to make some adjustments to the position and size of the form controls, and perhaps change the size of the form.

First close the hello_world application and then make the desired changes to the form and controls. Click the Start button to build and run the application again.

Tutorial Development Tools

This C Sharp Hello World tutorial was developed on a Windows 10 Home 64-bit computer using Visual Studio 2017 version 15.2 and Visual C# 2017.



One thought on “C Sharp Hello World Tutorial”

  1. how do i get the toolbox to have all the stuff inside it the one i have dosent have anything inside it?

Leave a Reply

Your email address will not be published. Required fields are marked *