Chapter 3. Fundamentals of Programming in Visual Basic

Chapter 3. Fundamentals of Programming in Visual Basic

Page 1 of 114

[Page 41]

Chapter 3. Fundamentals of Programming in Visual Basic

(This item omitted from WebBook edition)

3.1 Visual Basic Controls

42

z Starting a New Visual Basic Program

z A Text Box Walkthrough

z A Button Walkthrough

z A Label Walkthrough

z A List Box Walkthrough

z The Name Property

z A Help Walkthrough

z Fonts

z Auto Hide

z Positioning and Aligning Controls

3.2 Visual Basic Events

60

z An Event Procedure Walkthrough

z Properties and Event Procedures of the Form

z The Header of an Event Procedure

3.3 Numbers

73

z Arithmetic Operations

z Variables

z Incrementing the Value of a Variable

z Built-In Functions: Math.Sqrt, Int, Math.Round

z The Integer Data Type

z Multiple Declarations



9/11/2013

Chapter 3. Fundamentals of Programming in Visual Basic

Page 2 of 114

z Parentheses

z Three Types of Errors

3.4 Strings

88

z Variables and Strings

z Using Text Boxes for Input and Output

z Concatenation

z String Properties and Methods: Length Property and ToUpper, ToLower, Trim, IndexOf, and Substring Methods

z The Empty String

z Initial Value of a String

z Option Strict

z Internal Documentation

z Line-Continuation Character

3.5 Input and Output

105

z Formatting Output with Format Functions

z Formatting Output with Zones

z Reading Data from Files

z Getting Input from an Input Dialog Box

z Using a Message Dialog Box for Output

z Using a Masked Text Box for Input

Summary

127

Programming Projects

128

3.1. Visual Basic Controls

[Page 42]



9/11/2013

Chapter 3. Fundamentals of Programming in Visual Basic

Page 3 of 114

Visual Basic programs display a Windows-style screen (called a form) with boxes into which users type (and in which users edit) information and buttons that they click to initiate actions. The boxes and buttons are referred to as controls. In this section, we examine forms and four of the most useful Visual Basic controls.

Starting a New Visual Basic Program

For our purposes, Visual Basic programs are also known as applications, solutions, or projects. Each program is saved (as several files and subfolders) in its own folder. Before starting a new program, you should use Windows Explorer to create a folder to hold the folders for your programs.

The process for invoking Visual Basic varies slightly with the edition of Visual Basic installed on the computer. To invoke Visual Basic from a computer that has Visual Basic Express installed, click the Windows Start button, hover over All Programs, and then click on Microsoft Visual Basic 2005 Express Edition. With the other editions of Visual Basic, hover over All Programs, hover over Microsoft Visual Studio 2005, and then click on Microsoft Visual Studio 2005 in the short list that is revealed.

The window that appears after Visual Basic is invoked has a menu bar whose first item is "File". Click on File, and then click on New Project to produce a New Project input dialog box. Figure 3.1 shows the New Project input dialog box produced by Visual Basic Express. The "Windows Application" icon should be selected as the installed template. If this is not the case, click on "Windows Application" to select it. (The other editions of Visual Basic contain a pane identifying a Project type. You should select "Visual Basic" as the Project type.)

Figure 3.1. The Visual Basic Express New Project input dialog box. (This item is displayed on page 43 in the print version)

[View full size image]

Note: The number of project types and icons showing will vary depending on the version of Visual Basic you are using. Figure 3.1 was created from the Express Edition.



9/11/2013

Chapter 3. Fundamentals of Programming in Visual Basic

Page 4 of 114

The name of the program, initially set to WindowsApplication1, can be specified at this time. Since we will have a chance to change the name later, let's just call the program WindowsApplication1 for now. Click on the OK button to invoke the Visual Basic programming environment. See Figure 3.2. Note: Your screen will resemble, but may differ somewhat, from Figure 3.2. The Visual Basic programming environment is often referred to as the IDE (Integrated Development Environment).

Figure 3.2. The Visual Basic programming environment. (This item is displayed on page 43 in the print version)

[View full size image]

The Menu bar of the IDE displays the commands you use to work with Visual Basic. Some of the menus, like File, Edit, View, and Window, are common to most Windows applications. Others, such as Project, Build, and Debug, provide commands specific to programming in Visual Basic.

The Toolbars hold a collection of icons that carry out standard operations when clicked. For example, you can use the fifth icon, which looks like a stack of three diskettes, to save the files associated with the current program. To reveal the purpose of a Toolbar icon, hover the mouse pointer over the icon for a few seconds. The little information rectangle that pops up is called a tooltip.

In Figure 3.2, the Main area currently holds the Windows Form Designer. The rectangular Form window, or form for short, becomes a Windows window when a program is executed. Most information displayed by the program appears on the form. The information usually is displayed in controls that the programmer has placed on the form.

[Page 44]

The Solution Explorer window is used to display various parts of a program. The Properties window is used to change how objects look and react.

The Toolbox holds icons representing controls that can be placed on the form. These controls are



9/11/2013

Chapter 3. Fundamentals of Programming in Visual Basic

Page 5 of 114

grouped into categories such as General, Dialogs, Printing, etc. Figure 3.3 shows the Toolbox after the plus sign to the left of "Common Controls" has been clicked. Nearly all the controls discussed in this text can be found in the list of common controls. The four controls discussed in this chapter are text boxes, labels, buttons, and list boxes.

Figure 3.3. The Toolbox's common controls.

Note: If your screen does not show the Toolbox, move the mouse over the tab marked Toolbox at the left side of the screen. The Toolbox will appear. Then click on the pushpin icon in the title bar of the top of the Toolbox to keep the toolbox from sliding out of the way when the cursor is moved away from the Toolbox.

Text boxes: You use a text box to get information from the user, referred to as input, or to display information produced by the program, referred to as output.

Labels: You place a label near a text box to tell the user what type of information is displayed in the text box.

[Page 45]

Buttons: The user clicks a button to initiate an action.

List boxes: In the first part of the book, we use list boxes to display tables or several lines of output. Later, we use list boxes to make selections.



9/11/2013

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download