The Basic Use of Controls: The Hello World Program
A great tradition in introductory programming books is the Hello World program, which demonstrates how the simplest utility can be built and run in a given programming language. In the days of character-based programming, Hello World was usually a two-line or three-line program typed in a program editor and assembled with a stand-alone compiler. With the advent of complex operating systems and programming tools, however, the typical Hello World has grown into a more sophisticated program containing dozens of lines and requiring several programming tools for its construction. Fortunately, creating a Hello World program is still quite simple with Visual Studio and Visual Basic 2005. You can construct a complete user interface by creating two objects, setting two properties, and entering one line of code. Give it a try.
Create a Hello World program
Start Visual Studio 2005 if it isn't already open.
On the File menu, click New Project.
Visual Studio displays the New Project dialog box, which prompts you for the name of your project and for the template that you want to use.
NOTE
Use the following instructions each time you want to create a new project on your hard disk.
Ensure that the Visual Basic project type and the Windows category are selected, and then click the Windows Application template.
These selections indicate that you'll be building a stand-alone Visual Basic application that will run under Windows.
Remove the default project name (WindowsApplication1) from the Name text box, and then type MyHello.
NOTE
Throughout this book, I ask you to create sample projects with the My prefix, to distinguish your own work from the practice files I include on the companion CD-ROM.
The New Project dialog box now looks like this:
Click OK to create your new project.
The new project is created, and a blank form appears in the Designer, as shown in the following illustration. The two controls you'll use in this exercise, Button and TextBox, are visible in the Toolbox, which appears in the illustration as a docked window. If your programming tools are configured differently, take a few moments to organize them as shown in the illustration. (Chapter 1, “Exploring the Visual Studio Integrated Development Environment,” describes how to configure the IDE if you need a refresher course.)
Click the TextBox control on the Common Controls tab of the Toolbox.
Draw a text box similar to this:
Text boxes are used to display text on a form or to get user input while a program is running. How a text box works depends on how you set its properties and how you reference the text box in the program code. In this program, a text box object will be used to display the message “Hello, world!” when you click a button object on the form.
NOTE
Readers who have used earlier versions of Visual Basic will notice that the TextBox control no longer contains a default Text property value of “TextBox1” as it did previously. The default text box is now empty.
You'll add a button to the form now.
Click the Button control in the Toolbox.
Draw a button below the text box on the form.
Your form looks something like this:
As you learned in Chapter 2, “Writing Your First Program,” buttons are used to get the most basic input from a user. When a user clicks a button, he or she is requesting that the program perform a specific action immediately. In Visual Basic terms, the user is using the button to create an event that needs to be processed in the program. Typical buttons in a program are the OK button, which a user clicks to accept a list of options and to indicate that he or she is ready to proceed; the Cancel button, which a user clicks to discard a list of options; and the Quit button, which a user clicks to exit the program. In each case, you should use these buttons in the standard way so that they work as expected when the user clicks them. A button's characteristics (like those of all objects) can be modified with property settings and references to the object in program code.
Set the following property for the button object by using the Properties window:
Object | Property | Setting |
Button1 | Text | “OK” |
For more information about setting properties and reading them in tables, see the section entitled “The Properties Window” in Chapter 1.
Double-click the OK button, and type the following program statement between the Private Sub Button1_Click and End Sub statements in the Code Editor:
TextBox1.Text = "Hello, world!"
NOTE
After you type the TextBox1 object name and a period, Visual Studio displays a list box containing all the valid properties for text box objects, to jog your memory if you've forgotten the complete list. You can select a property from the list by double-clicking it, or you can continue typing to enter it yourself. (I usually just keep typing, unless I'm exploring new features.)
The statement you've entered changes the Text property of the text box to “Hello, world!” when the user clicks the button at run time. (The equal sign (=) assigns everything between the quotation marks to the Text property of the TextBox1 object.) This example changes a property at run time—one of the most common uses of program code in a Visual Basic program.
Now you're ready to run the Hello World program.
Run the Hello World program
TIP
The complete Hello World program is located in the c:\vb05sbs\chap03\hello folder.
Click the Start Debugging button on the Standard toolbar.
The Hello World program compiles and, after a few seconds, runs in the Visual Studio IDE.
Click OK.
The program displays the greeting “Hello, world!” in the text box, as shown here:
When you clicked the OK button, the program code changed the Text property of the empty TextBox1 text box to “Hello, world!” and displayed this text in the box. If you didn't get this result, repeat the steps in the previous section, and build the program again. You might have set a property incorrectly or made a typing mistake in the program code. (Syntax errors appear with a jagged underline in the Code Editor.)
Click the Close button in the upper-right corner of the Hello World program window to stop the program.
NOTE
To stop a program running in Visual Studio, you can also click the Stop Debugging button on the Standard toolbar to close the program.
Click the Save All button on the Standard toolbar to save your new project to disk.
Visual Studio now prompts you for a name and a location for the project.
Click the Browse button.
The Project Location dialog box opens. You use this dialog box to specify the location of your project and to create new folders for your projects if necessary. Although you can save your projects in any location (the My Documents\Visual Studio 2005\Projects folder is a common location), in this book I instruct you to save your projects in the c:\vb05sbs folder, the default location for your Step by Step practice files. If you ever want to remove all the files associated with this programming course, you'll know just where the files are, and you'll be able to remove them easily by deleting the entire folder.
Click the Desktop icon in the Project Location dialog box, double-click the My Computer icon, and then browse to the c:\vb05sbs\chap03 folder.
Click the Open button to open the folder you specified.
Clear the check mark from the Create Directory For Solution check box if it is selected.
Because this solution contains only one project (which is the case for most of the solutions in this book), you don't need to create a separate root folder to hold the solution files for the project. (However, you can create an extra folder if you want.)
Click Save to save the project and its files.
Congratulations—you've joined the ranks of programmers who've written a Hello World program. Now let's try another control.