Getting Started with Visual Basic
We are now going to run Visual Studio Community 2017 for the first time and write a "Hello World" program, in keeping with programming tradition! When you start the IDE for the first time, you are given the option to sign in to your Microsoft account. If you don't have one, you should take the time to set one up (it's free), if only to prevent you from receiving a message after 30 days telling you your trial period has expired!
Once you have signed in to your Microsoft account (or created a new account) Microsoft will update your license details, and you should see a screen like the one shown below.
Close the account settings screen by clicking on the Close button.
The first time you run Visual studio 2017, you will be asked to set up your development environment. Change the development settings from "General" to "Visual Basic" using the drop down menu provided. You can also choose between different colour schemes. We suggest you accept the default colour scheme for now (you can always change things later if you wish).
Click on the Start Visual Studio button. You should see a screen telling you Visual Studio is being prepared for first use.
The Visual Studio 2017 Start Page will now appear. From here, you can create a new project or open an existing project. Note the Solution Explorer window on the right-hand side of the screen, which is currently blank. This window displays the contents of a solution, including the solution's projects and each project's items.
Note: The Visual Studio integrated development environment is highly customisable. You can change the layout and appearance of the IDE to suit your personal requirements. The screenshots used in these pages were acquired using the default environment settings and color scheme, on a desktop computer running Windows 10.
This is probably a good point at which to clarify exactly what a 'solution' is. You may already know that a project is a collection of files and other resources required for a single program. A solution is a group of closely related projects. A solution must be explicitly created in Visual Studio 2017 before you can add new or existing projects to it. Each of the example programs described in these pages consists of a single project - we will not be creating solutions as such.
Create a new project now by clicking on the Create new project... link on the start menu, or by selecting New Project... from the File menu, as shown below.
Select Windows Forms App (.NET Framework) from the available options (see the illustration below), and click on OK.
For now, accept the default project name WindowsApp1. Visual Studio will tell you that it is creating the project.
Once the project has been created, you will see a screen like the one illustrated below. The form design window takes up most of the screen, and contains a single form labelled Form1. On the right of the screen you will see the Solution Explorer window and the Properties window.
A Visual Basic project is a collection of files related to a single program. The project will produce some compiled output (e.g. an executable program or class library), and will include files containing source code and documentation. To create our simple "Hello World!" program, we will be placing a single control on the form and adding some program code to it. We will also change some of the form's properties to make the application look a bit more professional.
Follow the steps below to complete the application.
At the top left-hand side of the form design window, you will see a vertically aligned tab labelled Toolbox. Click on this tab once, and you will see a window with a list of the available controls. By default, the All Windows Forms tab will be open (if not, click on the right-pointing arrow next to the tab to open it).
- Double-click the Button control in the Toolbox window. The Toolbox window should close automatically (if not, click on the close button at the top right-hand side of the Toolbox window). You will see a default-sized command button object appear on the form, with the name "Button1" (because it is the first button created).
- Button1 should already be selected (if so, it is displayed surrounded by small white boxes as shown above). If it is not currently selected, click on it once to select it. You will see the Properties window change to display the properties for the button.
- Change the Size property to read "100,23" (you may need to scroll up or down within the properties window to locate the property - note that properties are arranged in alphabetical order) . Once you have done this, you will see that the button has increased in length.
- Change the Text property to read "Please Click Me!" (don't include the double quotes).
- Click on Button1 once more to give it the focus, then click on Format in the menu bar at the top of the main window to open the drop down Format menu.
- From the Format menu, select Center in Form ► Horizontally.
- Open the Format menu once more and select Center in Form ► Vertically.
- Click anywhere on the form away from Button1 to display the form's properties in the Properties window.
- Set the form's Text property to read "Hello World!"
- Double-click the Button1 object. The code editor will open, and you should see the following code:
- The system has generated code automatically to handle a click event for Button1. Add the following program statement in the blank line preceding the End Sub statement:
MsgBox ("Hello World!", , "Hello World!")
The code you entered is a program instruction that creates a message box when Button1 is clicked. It displays the message "Hello World!" (the first parameter passed to MsgBox). The message box also displays the label "Hello World!" (the third parameter passed to MsgBox) at the top of the message box, signifying that it belongs to the "Hello World!" application. Note that, although the second MsgBox parameter is not used, two commas separated by a space are required to act as a placeholder.
To run the program, click on the Start button (the small green right-facing arrow on the toolbar), or select Start Debugging from the Debug menu, or press the F5 key. Now click on the Please Click Me! button. If you have done everything correctly, you should see something like this:
You have successfully created a working Visual Basic application! Perhaps more importantly, you now know how to create a project, add a control to a form, modify the properties of controls and forms, format a control, and add some program code to a control. You will soon be using the skills you have learned here to create more complex applications.