Ana səhifə

Microsoft® Visual Basic 0 Programming Module


Yüklə 0.64 Mb.
tarix18.07.2016
ölçüsü0.64 Mb.

CIS 105 –Programming Module Visual Basic 6.0

Microsoft® Visual Basic 6.0 Programming Module
Microsoft® Visual Basic (VB) is a software development package designed for Windows. VB is a visual, events-driven, 32-bit programming language that evolved from the earlier DOS version called BASIC (Beginners' All purpose Symbolic Instruction Code). BASIC programming is accomplished in a text-only environment, while VB programming is executed in a graphical environment. In other words, VB can create Windows programs suitable for Win9X or NT systems and BASIC creates DOS programs.
Creating a Visual Basic Project
This project will perform arithmetical operations with two numbers. The window objects will consist of a Form with 1.) command buttons to initiate operations, clear entries, and exit the project, 2.) text boxes for entering numbers and displaying results, and 3.) text box labels. The project includes the following steps:

  • Create the window screen (the Form).

  • Create object controls.

  • Set property values for object controls.

  • Design and write the procedures to handle the user events.

  • Run the project and make an application.

  • Print the project.

The window screen design and the window objects are illustrated in Figure 1. They are also described in detail in Table 1. VB interfaces are created on forms by adding object controls. The forms become application windows. The object naming convention uses names beginning with lowercase abbreviations that identify the object type, followed by capitalized object names. The following individual objects will be created in the Form:



  • Two label text boxes and two number text boxes.

  • Four arithmetic command buttons to initiate the events.

  • A picture box to display the event results.

  • A New Entry command button to clear all entries.

  • An Exit command button to end the project.






Figure 1. Layout of the Graphical User Interface.
Table 1. General Descriptions of Object Controls and Objects Used in this Project.


Object Control

Description

Labels:


(lbl)
lblNum1

lblNum2


Used to identify information entered into text boxes or displayed in results.


Use these two labels for the input text boxes.

Text Boxes:

(txt)
txtNum1

txtNum2


Used to enter input.

Use these text boxes for the two numbers.

Picture Boxes:

(pic)
picBox

Used to display output results and graphics.

Use this box for the mathematical operation answers.

Command buttons:

(cmd)
cmdAdd, cmdSub,

cmdMul, cmdDiv


cmdNewEntry

cmdExit


Used to execute an event initiated by a command (process).


Use these command buttons to add, subtract, multiply, or divide the two numbers.
Use this command button to clear the boxes and allow input of new numbers.
Use this command button to exit the project or program.

Project & Form:

(prj) (frm)
prjJacobs

frmMath



The name of the Project.

The name of the Form.




Launching Visual Basic


  • Select Start > Programs > Microsoft Visual Studio 6.0 > Microsoft Visual Basic 6.0. The New Project dialog box should appear (Figure 2). In the New tab, double-click the Standard EXE icon. The initial VB screen should open (Figure 3). Using the selection handles, stretch the Project Form window to the size illustrated in Figure 4.




Figure 2. The Visual Basic New Project dialog box with the Standard EXE icon selected.

Menu bar

Tool bar


Toolbox

Form

Project Container window

Project Explorer window

Properties

window


Form Layout window


Figure 3. The Visual Basic screen after opening a New Project.

Figure 4. Increased Form Size. The Project Container window has been maximized. Note also that the Form is now stretched to 7500 x 5010.


  • Name your Project, your Form and the caption in the Form title bar as illustrated in Figure 5. Use the syntax conventions in Table 1 for this and all other naming to follow. For the Project name, use prj, for the Form use frmMath, and for the Form title bar use Arithmetic Project by . (Don’t use the brackets.) Note that the Project Explorer window is floated in Figure 5. You can rearrange, display, hide, float and dock your application windows as required. If the windows or the toolbox are not displayed, use the View menu to display them as needed.




  • In the Project Explorer window, click on the Project name and use the Name field in the Properties window to rename the Project.




  • In the Project Explorer window, select the Form and rename it in the Properties window (see Figure 5). Also, to create the caption in the Form title bar, modify the Caption property.

Properties window



Caption

property


Name field

Project Explorer window








Figure 5. Naming the Project, Form, and Form Caption.
Creating the User Interface



Label

PictureBox
TextBox
Command Button

Figure 6. The four main control objects used in this Project.



  • Create object controls for the two labels. Labels are used to describe the textbox contents. First click the Label tool, then click on the Form and drag to create the Label object. Use the sizing handles to adjust the size and placement. Note how the pointer changes when you are on a grid point, compared to between grid points.






  • With the new Label object control still selected, in the Properties window rename the control lblNum1. Change the Caption property to Enter First Number. Using the same procedures, add the second Label object control, lblNum2, and change the Caption property to Enter Second Number. Position the controls to match Figure 1.

  • Click somewhere in the Form window (but not on a Label) to select the Form. In the Properties window, change the frmMath background color property pop-up menu to facilitate viewing placement of the objects (Figure 7). Select the desktop color in the System tab, or blue in the Palette. The Form should now appear as illustrated in Figure 8.





Figure 7. Using the Properties window to modify the Form background color.



Figure 8. Labels added to the Form.




  • Next, create the other object controls described in Table 1 and in Figure 1. Use the appropriate tools. Make certain that each is the correct object type and precisely named.

  • Text Boxes. When creating the text boxes, scroll down the Properties window to the Text property field and delete the default text. Note that the Properties are sorted alphabetically.

  • Command buttons. When creating command buttons, scroll down the Properties window to the Caption property and type an ampersand followed by the operation label (i.e., for cmdSub use &Subtract, and for cmdNewEntry use &New Entry). Resize the Form to provide equal margins around your objects (see Figure 9). Center the text in the labels by using the appropriate property in the Property window. Remember, select, then format.


Saving the Project


  • To save a Project in VB 6.0 you need to save two files, the Form file and the Project file.

  • Save the Form file first. Select File > Save Form As. Name your Form frmMath.frm and click Save.

  • Select File > Save Project As. Name your Project (using your last name) prj.vbp and click Save. After this initial saving procedure, you need only click the Save Project toolbar button to save both files.



Figure 9. Resized Form after adding all the objects and centering the label text.
Programming the Form
The following steps create event procedures to process the user requests. Event procedures are written in the Code window.


  • Select View > Code. Views can also be changed using the buttons in the Project window (Figure 10). Double-clicking an object in the Form will reveal partial code written into the code window for that object. If you do this, delete the code now. Figure 11 illustrates code view.

View Code button


View Objects button



Figure 10. Changing Views using the View Menu.
In Code view, it is possible to enter the code needed for the commands. Comment lines, typically created to document the programming features, do not affect program execution. Comment lines are created by placing an apostrophe at the beginning of every line. In the code sample below, the comment lines are color coded blue. (Table 3 provides detailed explanations for the programming code


  • Enter the code from Appendix A, below, in the Code Window with your name in the first comment line. Then save your work again. The Project should now be ready to run.

End Button


Run Menu
Code Window

Figure 11. Code View in the Project Container Window.

Running the Project


  • Select Run > Start or press F5 (Figure 11). Test if you can add, subtract, multiply, and divide the two numbers you input into the textboxes. If you cannot, you will need to analyze the code for errors. When you respond to an error you enter the code window in debug mode. After making the necessary corrections, you should end the program and re-run it. To end the program, click the End button in the toolbar (Figure 11).


Printing Instructions

  • Run the Project. Enter 12 for both numbers. Add and subtract the two numbers. The results should display in the picture box. Grading will depend on the following criteria. Set up your desktop similar to Figure 4, with the Project Explorer window visible, and with your project name and form name visible, similar to those in Figure 5. Also, your caption and name should appear in the Project title bar. Press the Print Screen button. Open Microsoft Word and set the page orientation to landscape and the page margins to 1 inch. Paste the screen capture and size the screen capture to fit the page. Print the screen capture image from Word.



Modifying the Project

  • Note that the picture box is not large enough to display all four arithmetic operations at once. The next steps enlarge the picture box to display more results and move an object. Rearrange the objects as illustrated in Figure 12.

  • Run the project. Enter numbers and complete the four arithmetic operations to test your project. End running the Project. Return to Object view. Customize the fonts, colors, form and control object sizes, etc. to your own preferences. Save your changes.



Figure 12. Modified Project Window with enlarged picture box.
Creating an Application (.exe file)


  • Select File > Make prjXXX.exe. Name the Project arithmetic.exe and save.

  • Minimize all windows and open arithmetic.exe, your new application, from Windows Explorer and test it.




Printing Instructions


  • Open arithmetic.exe. Enter 24 for both numbers. Add, subtract, multiply, and divide the two numbers. The results should display in the picture box.




  • Press Print Screen. Print the screen capture image from Word in landscape orientation, full page. Your name should appear in the Project title bar, along with the results of your arithmetic operations in the previous step.




  • To print the Project Source Code, select File > Print. In the Print dialog box, select the Current Module radio button for the Range selections, select the Code check box in the Print What selector, and then click OK (Figure 13). In the Computer Lab, VBasic printouts may be sent to the main printer. Ask at the desk if you cannot find the printout. Make certain your name appears in the first line of the code.


Hand in the source code and your two screen captures (40 points).


Figure 13. Printing the Project source code.




Table 3. VISUAL BASIC 6.0. Some Statements, Methods and Terms Used.




Cls

Method used to clear the elements out of the picture box.



  • picBox.Cls

Command Button

The user clicks a command button to indicate (begin) an action




Concatenation (symbol &)

Used to join two strings together to form a new string.



  • “good” & ”bye” creating a new string goodbye


Dimension

Statement

Declares the type of variable used (String or Number).




End

Statement used to terminate the execution of the project.



  • End


EXIT

Click File menu option then click Exit to end the Visual Basic 6.0 application program.




Event Procedure

Indicates a specific event occurs to a specified object. Event procedure examples include: object_Click (object is clicked); object_KeyPress (a key is pressed while object has the “focus”)




Format Functions

Provide detailed control of how numbers, dates, and strings are displayed. Numbers can be made to line up uniformly and be displayed with dollar signs, commas, and a specified number of decimal places. Dates can be converted to a long form. Strings can be right-justified.



Function (VAL)

Val Function used to convert a string into a number. (Numbers are stored in text boxes as strings and need to be converted to a number before assigning to numeric variables). Common usage would be to convert the string to a number to use in a calculation.



General Declaration

Section used to include comments that apply to the entire project.



IF…Then

Else…

End IF

Decision control structure that determines the course of action that the program will take based upon whether a condition is true or false.



  • If Condition Then

Action1 (Action1 taken if condition is True)

Else


Action 2 (Action 2 taken if condition is False)

End If


Method

Visual Basic methods such as Print and Cls (clear screen) are applied to objects and are coded as object.Print and object.Cls.



Object Control

Forms and controls (i.e. text boxes, labels, command buttons, picture boxes, and the form itself).



Open


To open a new project:

  • Open a New Project from the New tab by double-clicking the Standard EXE icon.

To open an existing project:

  • Open Visual Basic 6.0 and click on the Existing tab. In the look in box click the down arrow and select the drive. Make sure that the File Type on the bottom is set to Project Files. Then select the file.

  • To open the project, just click on the Open button.

  • Open the Form folder (click the +), then double-click the Form to open.

Option Explicit

Used in General Declaration Section to assure that all variables are declared before using.



Print

Method used to display data on the screen or printer.



Private Sub

Indicates the beginning of an event procedure




Property

An attribute for a specific object. Examples include:



  • Text (set the text displayed by a text box)

  • Caption (set the title of a form or the contents of a label)

  • Font size (set the size of the characters displayed)

  • Alignment (set the placement of the contents of a label)

  • Picture (display drawing in a picture box)

SetFocus

Method used to set the focus to the named control such as a textbox. Indicates where the cursor will appear.



  • txtNum1.SetFocus

Sub/End Sub

Sub procedures are used to carry out program tasks. Sub procedures are used to handle events as when the user clicks on a control button.




Value

Values are assigned to variables by assignment and Input # statements. The values appearing in assignment statements can be constants, variables, or expressions.



Variables

Variables are used to hold data. Variable names in Visual Basic must begin with a letter and can consist of letters, digits and underscores. Dim statements explicitly declare variables and specify the types of variables. Examples of variable types include Single, Integer, and String.


For continued reading, do a Google Web search for Visual Basic tutorials.


Credits. This programming module was produced by modifying the Visual Basic programming module created by MCC faculty members, Linda Cronquist and Angeline Surber. However, any errors in this version are my responsibility.
Appendix A. Programming Code.
'Programmer's Name

Option Explicit


Private Sub cmdAdd_Click()

'subroutine to add the two numbers together and display the results

Dim resultAdd As Single

resultAdd = Val(txtNum1.Text) + Val(txtNum2.Text)

picBox.Print

picBox.Print "Addition: " & txtNum1.Text & " + " & txtNum2.Text & " = "; resultAdd

End Sub
Private Sub cmdSub_Click()

'subroutine to subtract the two numbers and display the results

Dim resultSubtract As Single

resultSubtract = Val(txtNum1.Text) - Val(txtNum2.Text)

picBox.Print

picBox.Print "Subtraction: " & txtNum1.Text & " - " & txtNum2.Text & " = "; resultSubtract

End Sub
Private Sub cmdMul_Click()

'subroutine to subtract the two numbers and display the results

Dim resultMult As Single

resultMult = Val(txtNum1.Text) * Val(txtNum2.Text)

picBox.Print

picBox.Print "Multiplication: " & txtNum1.Text & " X " & txtNum2.Text & " = "; resultMult

End Sub
Private Sub cmdDiv_Click()

'subroutine to divide the two numbers and display the results

Dim resultDivide As Single

'checks to see if the divisor is equal to zero and if so, display a message

If Val(txtNum2.Text) = 0 Then

picBox.Print

picBox.Print "Division by zero - please re-enter "

Else


'process the division of the two numbers

resultDivide = Val(txtNum1.Text) / Val(txtNum2.Text)

picBox.Print

picBox.Print "Division: " & txtNum1.Text & " / " & txtNum2.Text & " = "; resultDivide

End If

End Sub
Private Sub cmdNewEntry_Click()



'used to clear the boxes and set the cursor back to number one

picBox.Cls

txtNum1.Text = ""

txtNum2.Text = ""

txtNum1.SetFocus

End Sub
Private Sub cmdExit_Click()

'to end the project

End


End Sub






Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©atelim.com 2016
rəhbərliyinə müraciət