Who is the end user?An end user is the person for whom the program was made--the person who uses the program.
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.
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 controlsAnother 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.
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
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.
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
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.
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.
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 IDEAnimation, 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
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.
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
FIGURE 1.9 The Properties window is where you configure the settings for a selected ActiveX control.
Measuring a second with the TimerYou 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.
Add a Label control to the form
FIGURE 1.10 You also can add a control to a form by selecting the control in the ToolBox and dragging to size.
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
Label1.Caption = Time
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
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 propertyAll 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 codeIf 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.
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
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 projectsWhen 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.
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."
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
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
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.
Restrict resizing and assign a title to the form
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.
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."
© Copyright, Macmillan Computer Publishing. All rights reserved.