Getting Started with Visual Basic

We are now going to run Visual Studio Community 2022 for the first time and write a "Hello World" program, in keeping with programming tradition! When you start the IDE for the first time, the Visual Studio 2022 Start Page will now appear. From here, you can create a new project or open an existing project.


The Visual Studio 2022 start page.

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 11.

Create a new project now by clicking on the Create a new project box. This brings up the Create a new project windows, as shown below, which allows you to choose the type of project you want to create. Note that in future, you can create a new project either by clicking on the "Create a new project" box on the Visual Studio Start Page, or by selecting New > Project... from the File menu.


The 'Create a new project' window

Scroll down through the options (there are quite a few!) and select Windows Forms App. Make sure you select the Visual Basic version - see the illustration above - and click on the Next button. You should now see the "Configure your new project" dialog box.


Select Windows Forms App (.Net Framework) from the available options

For now, accept the default project name WindowsApp1. You may now see the following "Additional Information" screen, which allows you to choose between long term and standard term support. For now, just click on the Create button.


Additional Information screen

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.


Creating project WindowsApp1 . . .

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.

  1. From the View menu, select Toolbox. You will now see a window with a list of the available controls. Click on the right-pointing arrow next to the All Windows Forms tab to open it.


    The toolbox window.

  2. 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 has been placed on the form

  3. 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, then right-click on the button and select Properties from the drop-down menu. The Properties window will appear underneath the Solution Explorer window, and will display the properties for the button.



    The Button1 property window

  4. Change the Size property to read "100,23" (you may need to scroll up or down within the properties window to locate the property). Once you have done this, you will see that the button has increased in length.
  5. Change the Text property to read "Please Click Me!" (don't include the double quotes).
  6. 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.


    The Format menu

  7. From the Format menu, select Center in Form Horizontally.


    Select Center in Form, Horizontally

  8. Open the Format menu once more and select Center in Form Vertically.
  9. Click anywhere on the form away from Button1 to display the form's properties in the Properties window.
  10. Set the form's Text property to read "Hello World!"
  11. Double-click the Button1 object. The code editor will open, and you should see the following code:

The code editor window
  1. 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:


The Hello World! message box

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.