'; zhtm += ''; zhtm += '

' + pPage + ''; zhtm += ''; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 } //--> Using Visual Basic 6 -- Ch 1 -- Building Your First Application


Using Visual Basic 6

Previous chapterNext chapterContents


- 1 -
Building Your First Application



Who is the end user?

An end user is the person for whom the program was made--the person who uses the program.


Writing Your First Application

The best way to learn programming is to do programming. Therefore, let's create a program. Your first program will be a Visual Basic Calendar, which allows the end user to

If you think that this is too much functionality to implement for a beginner's program, don't worry; it's not. Visual Basic does most of the work for you. Unlike other languages, such as C++, where you have to program every little thing, Visual Basic brings a high degree of automatic programming to your fingertips. Thus, you can do a lot with not a lot of code.

However, don't misinterpret "not a lot of code" to mean "not a lot of power." Visual Basic is a very powerful language. You can write Visual Basic code that does "every little thing" if you want or need it to. You also can exploit the labor-saving nature of VB to implement a program quickly. The choice is yours. Visual Basic is versatile enough to be useful to a broad range of programmers--from beginner to seasoned professional.

Starting Visual Basic

The first thing you need to do is open a Visual Basic program. From the Windows Start menu, choose Programs, Microsoft Visual Studio 6.0, and then Microsoft Visual Basic 6.0.

When you start VB, you're taken to the Visual Basic Integrated Development Environment (IDE). This is your programming workbench, the place where you create your programs (see Figure 1.1). Notice that the IDE presents what looks like an empty window containing some dots and a title bar with the word Form1. This window is a form, the primary building block of a Visual Basic application. An application can have many forms or one form; the application we're developing for this example has one form.

FIGURE 1.1 The Visual Basic Integrated Development Environment presents a unified programming work area.


ActiveX controls

Another name for the little programs that come with Visual Basic is ActiveX controls. The calendar program that you add to your application is an ActiveX control.


Building the Calendar Feature

Now let's build the features of your application. First, you need a calendar. You could build one from scratch, or you could add the calendar program that comes within VB to your application. Let's use the one that comes with VB (if you don't have a few years to learn VB at the expert level, it makes the most sense).

If you don't want to create the Visual Basic Calendar program from scratch, you can retrieve the finished code from the Web site dedicated to this book (http://www.mcp.com/info). You'll be asked to enter an ISBN; you need to enter 078971633x, and then click the Search button to go to the Book Info page for Using Visual Basic 6. After you download the code, double-click the file Project1.vbp for the Chapter 1 code. If you've installed Visual Basic on your system, double-clicking invokes the sample project within VB.

Get the calendar program into the Visual Basic IDE

1. From the Project menu, choose Components (see Figure 1.2).

FIGURE 1.2 All the ActiveX Controls that come with VB aren't automatically loaded by default. You have to select some from the Components menu.

2. In the Components dialog, select Windows Common Controls 2-6.0 and click OK (see Figure 1.3).

FIGURE 1.3 Visual Basic comes with many ActiveX components that you can use in your projects. Some need to be added to the IDE.


What is ActiveX?

ActiveX is a brand name from Microsoft that lets you create little programs, formally known as components and controls, that you can add to larger programs. These larger programs that you can add ActiveX components and controls to can be standalone programs or programs that run on the Internet. You can use Visual Basic to make your own ActiveX components and controls.


These steps added the calendar control to the Visual Basic ToolBox. The formal name for the control that you've inserted into your first application is the MonthView ActiveX control. We'll use this terminology from here on.

Now you need to add the control to the form.

Add the MonthView control to your form

1. Select the MonthView control icon from the Visual Basic ToolBox (see Figure 1.4).

2. Double-click the MonthView control icon to add the control to the main form (see Figure 1.5).

FIGURE 1.4 When you add an ActiveX Control from the Components dialog, it appears in the Visual Basic ToolBox.

FIGURE 1.5 When you add an ActiveX Control to a form, the control sets itself at its default size.

You've just created your first program. Press F5 or choose Start from the Run menu to run it . Click the End button or choose End from the Run menu to terminate the program.

Adding a Clock Feature

The next feature to implement into your practice program is the capability to display the time. You'll use the Timer ActiveX control, which is intrinsic, meaning that it's standard to Visual Basic (built right into the core code). You never have to add it to the ToolBox; the Timer is always there (see Figure 1.6) Look at Table 1.1 to see the ToolBox icons and their associated ActiveX control.

FIGURE 1.6 The Timer is a Visual Basic intrinsic control.

TABLE 1.1  The Visual Basic Controls

Icon ActiveX Control
Not a control; enables the mouse pointer
Label
TextBox
CommandButton
Timer
PictureBox
Frame
CheckBox
OptionButton
ComboBox
ListBox
Vertical scrollbar
Horizontal scrollbar
DriveListBox
DirListBox
FileListBox
Shape
Line
Data
OLE
Animation
UpDown
MonthView
DateTimePicker
FlatScrollBar


The latest additions to your IDE

Animation, UpDown, MonthView, DateTimePicker, and FlatScrollBar are all Windows common controls. When you selected Windows Common Controls 2-6.0 in the earlier steps, you put all these controls into your ToolBox.


Add the timer to your application

1. Make sure that the Pointer icon in the ToolBox is selected.

2. Put the mouse pointer on the MonthView control on the form, press your left mouse button down, and drag the control over to the right (see Figure 1.7).

3. Double-click the Timer control in the ToolBox to add it to the form (see Figure 1.8).

FIGURE 1.7 You can move an ActiveX control around a form at design time by dragging it.

FIGURE 1.8 Controls appear in the middle of a form when you add them by using the double-click technique.


What's a windowless control?

If the Timer appears over the MonthView ActiveX control, don't fret; the Timer is a windowless control. A windowless control appears during design time but is invisible when you run your code. Don't mistake controls that can be made invisible at runtime as windowless controls; they aren't. You never see a windowless control at runtime.


Configuring the Timer Control

Although you've added the Timer to the form, it won't really do anything until you configure it to report time the way you want it to.

Configure the Timer to report the time

1. Select the Timer control on the form. (Make sure that the Pointer is selected in the ToolBox.)

2. In the Properties window, select the Interval property and enter the number 500 (see Figure 1.9).

FIGURE 1.9 The Properties window is where you configure the settings for a selected ActiveX control.


Measuring a second with the Timer

You set the increment of time measured by the Timer by using the Interval property. The Timer measures time in intervals of 1/1000 of a second. Therefore, to measure time in half-second intervals, set the value of the Interval property to 500.


Setting the value of the Timer's Interval property to 500 is operationally the same as configuring the Timer to do something every half a second. Now you have to program what you want the Timer to do every half second: to report the current time. You need a place in which to display the time. For this, use a Label control.

Using the Label Control to Display the Time

Add a Label control to the form

1. Select the Label control icon in the ToolBox.

2. Rather than double-click, draw a box on Form1 by dragging the mouse (see Figure 1.10).

FIGURE 1.10 You also can add a control to a form by selecting the control in the ToolBox and dragging to size.

3. In the Properties window, scroll to the BorderStyle property. Click the property and then select 1-Fixed Single from the drop-down list (see Figure 1.11).

FIGURE 1.11 Some properties, such as the Label control's BorderStyle property, have values that you can set from drop-down menus.


What's an event procedure?

An event procedure is a piece of code that you write to execute a programming event. For example, when a user clicks a CommandButton, a Click() event is generated. Visual Basic provides a CommandButton_Click() event procedure that you can program to react to the CommandButton click. The same is true with the Timer. When a Timer's interval is reached, a Timer() event is fired. You can think of the code that you write in the Timer() event as "do this when the Timer's interval has been reached." Some controls have many events; some have only a few. It depends on the control. You can learn more about event procedures in Chapter 6, "Working with Properties, Methods, and Events."


Code the instructions that display the time in the Label control

1. Double-click the Timer control on the form. The Code window appears.

2. Add the following line of code to the Timer1_Timer() event procedure in the blank space between the line Private Sub Timer1_Timer() and End Sub (see Figure 1.12):
Label1.Caption = Time


3. Run the code by pressing F5 (see Figure 1.13).

Adding a Message

For this program, a message appears when you click the CommandButton on the form. To implement this feature, first you must add a CommandButton to the form.

Add a CommandButton

1. Select the CommandButton control in the ToolBox.

2. Add a CommandButton to the form by dragging your mouse cursor across the form (see Figure 1.14).

FIGURE 1.12 The Timer event procedure is fired every time its Interval is reached. If you set the value of the Interval property to 500, the Timer event is fired every half second.

FIGURE 1.13 The Timer assigns the present time to the Caption property of the Label control in its Timer() event procedure.

FIGURE 1.14 Add a control by dragging if you want to custom-size it at a specific location.


A control's Name property

All controls have a unique name. By default, VB assigns the value of the Name property to be the control's type name plus a number. For example, the default value of the CommandButton's Name property is Command1. If the value Command1 is already in use, VB names the new CommandButton Command2 (and so on). You can change the default value of a control's Name property only at design time. Also, the Name property of the control isn't something that end users see when the program is running.



Using listing code

If you don't want to type code from the book into your programs, you can use the code in the file referenced in the heading of the listing. You can get this code from the book's Web site (http:// www.mcp.com/info). To access this code, open the file in a text editor such as Notepad. Then Copy and Paste the code from the text editor into VB.


3. Use the drag technique in Step 2 to add a TextBox above the CommandButton.

4. Double-click the CommandButton to expose the Click() event procedure.

5. Add the code in Listing 1.1 to the Command1_Click() event procedure (see Figure 1.15).

LISTING 1.1  01LIST01.TXT--The Code that displays a secret message in the
TextBox

01 Dim strMsg As String
02 strMsg = WeekdayName(Weekday(Date), _
False, vbUseSystemDayOfWeek)
03 strMsg = strMsg & " is the first day of the rest"
04 strMsg = strMsg & " of your life!"
05 Text1.Text = strMsg
6. In the Properties window, set the Caption property for the Command1 CommandButton to Click for a Secret (see Figure 1.16).

FIGURE 1.15 Notice that the code for the Command1_Click() event procedure doesn't have line numbers, which are used in this book for reference purposes. You shouldn't use them in code.

FIGURE 1.16 When you change the value of the CommandButton's Caption property, you change the text that the CommandButton displays within itself.


Saving projects

When you save a project, you should create a directory to hold all the parts of the project--project file (.prj) and forms (.frm and .frx). FRX files contain information about embedded graphics in a form.


7. Save the code by selecting Save Project from the File menu.

8. Run the code by pressing the F5 key (see Figure 1.17).

The code in Listing 1.1 shows you how to work with variables. You can think of a variable as a storage container for unknown information that's to be provided at a later time or at another place in code. Line 1 of Listing 1.1 shows you how to declare a variable by using the Dim keyword as shown in Line 1. Line 1 creates the variable strMsg.

When you create a variable, it doesn't contain any data. To give the variable some value, you must assign some data to it. Lines 2 through 4 of Listing 1.1 assign value to the strMsg variable by continually adding new data to existing data. Finally in Line 5, the information stored in the variable strMsg is assigned to be the value for the Text property of the Text1 TextBox.

FIGURE 1.17 Click the CommandButton to fire the Click() event procedure that displays the secret message in the TextBox.

For more information on variables, what they are, and how to use them, see Chapter 7, "Using Data Types, Constants, and Variables."

Improving Your First Application

As you review the work that you've done so far, notice that some things need improvement. Figure 1.18 shows these shortcomings.

FIGURE 1.18 The upper form is the state of your program before the user clicks the CommandButton. The lower form is after the user clicks the CommandButton.

Make the clock more readable

1. Press Shift+F7 to view the Object window.

2. Select the Label control in the main form.

3. In the Property window, scroll down to the Font property.

4. Click the ... button to the right of the Font property (see Figure 1.19).

5. Select the Bold Font Style from the Font dialog (see Figure 1.20).

FIGURE 1.19 Clicking an ... button opens a Property dialog.

FIGURE 1.20 The Font dialog appears when you need to change the Font property of any control.

Improve the TextBox

1. Select the TextBox.

2. Scroll down the Properties window to the MultiLine property. Set it to True as shown in Figure 1.21.

3. Keep scrolling down the TextBox properties to the ScrollBars property. Set it to 2-Vertical (see Figure 1.22).

FIGURE 1.21 Set the MultiLine property to True to make the text in the TextBox wrap.

FIGURE 1.22 If you set ScrollBars to 2-Vertical, up/down scrollbars appear. 3-Horizontal show sideways scrollbars, and 4-Both show scroll bars both ways.

4. Scroll to the Text property and delete the text Text1 from the drop-down list (highlight it and press Delete).

5. Increase the vertical size of the TextBox a small amount to accommodate the capability to display multiple lines of text.

Restrict resizing and assign a title to the form

1. Click the form. Be careful not to click any ActiveX control on the form; otherwise, you'll get the properties for the control rather than the form in the Properties window.

2. Select the BorderStyle property and set it to 3-Fixed Dialog (see Figure 1.23).

FIGURE 1.23 Setting the BorderStyle property to 3-Fixed Dialog makes it so that the form can't be resized. Also, only the Close (¥) button is shown in the title bar.

3. In the Properties window, set the form's Caption property to First App.

4. Save your work by choosing Save Project from the File menu. Then run the code by pressing F5 (see Figure 1.24).

FIGURE 1.24 Your first application lets you view months and the present time, as well as display a secret message that users can view by scrolling through a TextBox.

Congratulations! You've made a very powerful program in Visual Basic by using the tools and building blocks that the language provides. Granted, a lot is going on behind the scenes that we might have covered in more detail, but you'll get more in-depth coverage in the following chapters.

For more information about the intrinsic controls that come with VB, read Chapter 4, "Using the Intrinsic Controls." To learn how to get around the VB IDE, read Chapter 3, "Working in the Visual Basic 6 Programming Environment." To get a clear understanding of properties, methods, and events, and how they relate to Visual Basic programming, read Chapter 6, "Working with Properties, Methods, and Events." Finally to get an in-depth view of the Timer control, read Chapter 16, "Working with Time and Timers."


Previous chapterNext chapterContents

© Copyright, Macmillan Computer Publishing. All rights reserved.