Sei sulla pagina 1di 9

SANAL DERSANE -BY HAKAN KIRIK

Using Visual Basic 6


chapter: 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

-1Building Your First Application


Writing Your First Application Starting Visual Basic Building the Calendar Feature Adding a Clock Feature Configuring the Timer Control Using the Label Control to Display the Time Adding a Message Improving 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

View a calendar of the present month Browse months View a clock that presents the current time Read a secret message

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

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). Get the calendar program into the Visual Basic IDE 1. From the Project menu, choose Components 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

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 2. Double-click the MonthView control icon to add the control to the main form 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 Look at Table 1.1 to see the ToolBox icons and their associated ActiveX control. TABLE 1.1 The Visual Basic Controls 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 3. Double-click the Timer control in the ToolBox to add it to the form You can move an ActiveX control around a form at design time by dragging it. 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. 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 . 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. 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 :
Label1.Caption = Time

3. Run the code by pressing F5.

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. 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. The Timer assigns the present time to the Caption property of the Label control in its Timer() event procedure. 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.

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

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 .

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. 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. 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. 5. Select the Bold Font Style from the Font dialog. Improve the TextBox 1. Select the TextBox. 2. Scroll down the Properties window to the MultiLine property. Set it to True. 3. Keep scrolling down the TextBox properties to the ScrollBars property. Set it to 2Vertical. 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. 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.

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

HAKAN KIRIK