- Account
- Join for Free
- Sign In
- Help & Info
- Privacy Notice
- DMCA
- Contact Us
- Terms Of Use
VISUAL BASIC For Engineers & Scientists Shahab D. Mohaghegh, Ph.D. Professor Petroleum & Natural Gas Engineering West Virginia University March 1997 TABLE OF CONTENTS Chapter 1: Problem Solving with Visual Basic Introduction 1.1 Solving Problems with Visual Basic 1.2 Using a Five-Step Problem-Solving Process 1.3 The Five-Step Problem-Solving Process in Visual Basic Summary References Chapter 2: An Introduction to Visual Basic Introduction 2.1 Event Driven Programming 2.2 Visual Basic Environment 2.2.1 Menu Bar 2.2.2 Toolbar 2.2.3 Toolbox 2.2.4 The Form Window 2.2.5 Project Window 2.2.6 Properties Window 2.2.7 Code Window 2.2.8 Debug Window 2.3 Creating Graphical user Interface (GUI) 2.4 Objects and Controls 2.4.1 Object / Control Properties 2.4.2 Object / Control Procedures 2.5 Menus and Dialog Boxes 2.6 File Access 2.6.1 Opening and Closing Files from within a Program 2.6.2 Reading from a File 2.6.3 Writing to a File 2.7 Writing Code 2.6.1 General Declaration 2.8 Using On Line Help 2.9 Conventions Summary References Chapter 3: Engineering Computation with Visual Basic Introduction 3.1 Writing your First Program (Windows Application) The Hello Program 3.1.1 Writing Visual Basic Code 3.1.2 Improving the First Program The New Hello Program 3.1.3 Try It!
3.2 Visual Basic Variables and Operators 3.2.1 Data Types 3.2.2 Variable ... more.
less.
Declaration 3.2.2.1 Local and Global Variables 3.2.2.2 Constants 3.2.3 Operators in Visual Basic The Operator Program 3.2.3.1 Code Interpretation 3.2.4 Operators Combination and Formulas 3.2.4.1 Operator Precedence 3.2.4.2 Mathematical Functions The Functions program 3.2.4.3 Code Interpretation 3.2.5 Other Operators 3.2.6 Try It! 3.3 Visual Basic Decision Structures 3.3.1 If ... Then Decision Structure The New Operator Program 3.3.1.1 Code Interpretation 3.3.2 Select - Case Decision Structure The Enhanced Operator Program 3.3.2.1 Code Interpretation 3.3.3 Try It!<br><br> APPLICATION: Electrical Engineering APPLICATION: Chemical Engineering Summary References Chapter 4: File Access and Engineering Graphs with Visual Basic Introduction 4.1 Visual Basic Loops and Arrays 4.1.1 For...Next Loops 4.1.2 Do Loops 4.1.3 Try it! 4.2 File Access in Visual Basic 2.6.1 Opening and Closing Files from within a Program 2.6.2 Reading from a File 2.6.3 Writing to a File 4.3 Engineering Graphs with Visual Basic 4.3.1 Histograms 4.3.2 Pie Charts 4.3.3 XY plots 4.3.4 Try It! APPLICATION: Petroleum Engineering APPLICATION: Mechanical Engineering Summary References Chapter 5: Engineering Applications with Visual Basic APPLICATION: ELECTRICAL ENGINEERING Equipment Reliability Simulation Data Electric Circuit Model Spacecraft Data Analysis APPLICATION: CHEMICAL ENGINEERING Temperature Conversion Waste Water Treatment APPLICATION: CIVIL ENGINEERING Simply Supported Beams Earthquake Measurements APPLICATION: MECHANICAL ENGINEERING Stride Estimation Composite Materials Accelerometer Model APPLICATION: PETROLEUM ENGINEERING Oil Well Production Oil and Gas Property Evaluation APPLICATION: AEROSPACE ENGINEERING Wind Tunnel Computation of Lift and Drag Forces Terrain Navigation APPLICATION: ENVIRONMENTAL ENGINEERING Timber Growth SCIENTIFIC APPLICATIONS Bacteria Growth (Biology) Carbon Dating (Archeology) Light Pipes (Physics) Climatology Data (Meteorology) Roots of Equations (Mathematics) Visual Basic for Engineers & Scientists 1 Visual Basic Environment Learning a programming language is an essential part of any engineering education.<br><br> Much can be said about usefulness and necessity of programming languages in an Engineering curriculum. One thing that usually cannot be said about programming languages such as Fortran, C, or C++ is that, cthey are exciting d. What sets Visual Basic apart from other high level programming languages, is the excitement it initiates in those that are learning it.<br><br> This is of special importance to freshman engineering students, since it contributes to the very important issue of retention. Visual Basic provides a rich set of technically oriented commands and functions that will prove more than sufficient for solving any engineering problems during a 4 year engineering program, as well as a graduate program. Visual Basic Environment In this chapter you will explore the Visual basic environment.<br><br> A unique programming environment that at the beginning can be a bit intimidating, but once you have completed this chapter, you will find that it is actually a rich and user friendly environment. Lets us start with the first dialog box that you will encounter once you start Visual Basic. Figure 1-1 shows the Visual Basic 9s New Project dialog box.<br><br> Using this dialog box you will initiate your first project in Visual Basic. As you can see this dialog box has three tabs. The New tab lets you initiate a new application.<br><br> In this course you will mostly create Standard.EXE applications, i.e. stand alone windows programs. Shahab D.<br><br> Mohaghegh Chapter ONE -1 Visual Basic for Engineers & Scientists Figure 1-1. Visual Basic 9s New Project dialog box. Visual Basic also allows you to create other applications such as ActiveX controls and DLLs (Dynamic Link Libraries 3 sharable subprograms used by other programs) or other applications as viewed in this dialog box.<br><br> There are two other tabs. Existing tab lets you open existing Visual Basic projects to edit or modify, while the Recent tab provides you with a list of most recently created or edited Visual Basic projects. To start a new project, click on the Standard.EXE icon and then click on the O pen button.<br><br> The window shown in Figure 1-2 will open. We now explore the components of this window one by one. The Menu bar is at the very top.<br><br> Menu bar contains most of the utilities that are available to you in the Visual Basic environment. Such as F ile, E dit, V iew, etc. Click on each of these items to see the available utilities.<br><br> Shahab D. Mohaghegh Chapter ONE -2 Visual Basic for Engineers & Scientists Figure 1-2. New project environment.<br><br> Under the Menu bar is the Tool bar. Tool bar contains some of the most often used items from the Menu bar in small icons. Move the mouse pointer on each icon and leave it for a moment and the function of the icon will appear in a small sidebar.<br><br> Figure 1-3. Visual Basic Environment Menu bar. Shahab D.<br><br> Mohaghegh Chapter ONE -3 Visual Basic for Engineers & Scientists Figure 1-4. Visual Basic environment Tool bar. The Toolbox in the Visual Basic environment contains the controls (also referred to as objects) that are most often used in developing applications.<br><br> Throughout this textbook you will learn how to use all these controls. Figure 1-5. Visual Basic environment Toolbox.<br><br> In the following table the controls in the toolbox will be introduced one at a time. In the following chapters these controls will be used in different applications and their properties will be explored. Pointer It does not draw a control.<br><br> Pointer, lets you select, resize or move a control already on a form. Picture Box Displays graphics, as a container that receives output from graphics and print methods. Label Displays text that can not be changed, like caption under a graphic.<br><br> It can be changed by program codes. Shahab D. Mohaghegh Chapter ONE -4 Visual Basic for Engineers & Scientists Text Box Holds text that the user can either enter or change, or text generated by application.<br><br> Frame Allows you to create a visual or functional grouping for controls. Draw the Frame first, and then draw controls inside the frame, to form a group. Command Button Creates a button the user can choose (click) to carry out a sequence of instructions.<br><br> Check Box To choose between yes/no, true/false, and include/ exclude or multiple choices when more than one item may be chosen. Option Button This control is used to select one item from a group of items. Combo Box Combines the features of list box and text box.<br><br> The user can either choose an item from the list or enter a value in the text box. List Box Displays a list of items from which the user can choose one. Timer Used to activate a specific event at set intervals.<br><br> This control is invisible at run time. VScrollBar (vertical scroll bar) Provides a graphical tool for moving through lists or selecting data ranges. HScrollBar (horizontal scroll bar) Provides a graphical tool for moving through lists or selecting data ranges.<br><br> DriveListBox Displays valid disk drives at run time. DirListBox (directory list box) Displays directories and paths at run time. Shahab D.<br><br> Mohaghegh Chapter ONE -5 Visual Basic for Engineers & Scientists FileListBox Displays a list of files at run time. Shape Used to draw a variety of shapes such as a rectangle, square, rounded square, oval, or circle, etc. Line Used to draw a variety of line styles on your form at design time.<br><br> Image Displays a graphical image from a bitmap, icon, metafile as well as JPG or GIF files. It is decorative and uses fewer resources than a PictureBox. Data Provides access to data in databases through bound controls on your form.<br><br> OLE Allows you to link and embed objects from other applications in your Visual Basic application. Table 1-1. Most frequently used controls in the Visual Basic environment.<br><br> There are three other windows that appear in the Visual Basic environment. They are Project window, Properties window and the Layout window. Project window provides an explorer type view of all the forms and modules in the project, while Properties window provides a list of all the properties available for each control with their corresponding value.<br><br> Shahab D. Mohaghegh Chapter ONE -6 Visual Basic for Engineers & Scientists Figure 1-6. Visual Basic 9s Project window.<br><br> Layout window shows the relative location of the forms on the screen. As you change the location and size of the forms the changes are reflected in the Layout window. Figures 1-6 and 1-7 shows Visual Basic 9s Project, Properties and Form Layout windows.<br><br> You can change the location of the application window (the form) by moving its location in the Form Layout window. To change the location of the form in the Form Layout window simply click on it (where its written Form1) and drag it to any location in the in the monitor 9s picture. Figure 1-7.<br><br> Visual Basic 9s Properties and Form Layout windows. Shahab D. Mohaghegh Chapter ONE -7 Visual Basic for Engineers & Scientists Form window, shown in Figure 1-8, is the main window in the Visual Basic environment.<br><br> This is the window within which the application interface is developed. You develop the application interface by placing controls in the Form. Figure 1-8.<br><br> Visual Basic 9s Form window. You can change the size of the form by clicking on the handles and dragging them. Once the controls are placed in this window and their properties are assigned in the Properties window it is time to attach codes to each control as needed.<br><br> Attaching code to the control means writing the sequence of instructions that pertains to each control. In order to write the code for each control you must first bring up the code-editing window for that control. You can do that by double clicking on the control.<br><br> If you wish to attach code to a Form, you should double click on the Form itself. Shahab D. Mohaghegh Chapter ONE -8 Visual Basic for Engineers & Scientists Figure 1-9.<br><br> Visual Basic 9s code editing window. Once the code edit window appears, the control that the code is being written for is shown in the top left Combo Box and the event procedure is shown in the top right Combo Box. Visual Basic is an event driven programming language.<br><br> This simply means that the applications that are developed using Visual Basic, much like any other windows applications, will act upon the users actions (event procedures) such as Click, DoubleClick, DragDrop, MouseDown, etc. Whenever you want the control in your application to respond to an event, you put the instructions in the appropriate event procedure. Of course the user does not initiate all the events.<br><br> Sometimes events are consequence of other events. For example as you start an application the main Form is loaded in the memory and shows up on the screen. This is a Form_Load event, which simply means that the Load event procedure is activated on the Form control.<br><br> Using the Shahab D. Mohaghegh Chapter ONE -9 Visual Basic for Engineers & Scientists arrow in the left Combo Box will show all the controls that are used in the application and clicking on the arrow of the right Combo Box shows all the available procedure for the control that is shown on the left Combo Box. Control Properties Every control in a Visual Basic application has certain characteristics called properties.<br><br> These properties dictate the way a control looks, size, color, and screen location among other things. When you place a control in your application (either by drag and drop or by double clicking on it) the property window will display the properties corresponding to that control. There is a default value for each property.<br><br> You may change these values to fit the purposes of your application. Once a control is placed on a Form, Visual Basic automatically will issue a name for that control. This is the name that the control will be known by, to the rest of the application.<br><br> When you write codes for that control you notice that the name that Visual Basic has given to this control appears in the top left Combo Box of the code edit window. This name can be changed, just like any other property of the control, from the Properties window. The Name property is the first property that appears in the Property window.<br><br> Figure 1-10 shows a form that has a Command Button control placed in it. The Property window for the Command Button control is also shown in this Figure. Shahab D.<br><br> Mohaghegh Chapter ONE -10 Visual Basic for Engineers & Scientists Figure 1-10. Properties of the Command Button Once a control like the Command Button control is placed on the Form, it has cCommand1 d written on it. This is called the caption.<br><br> This may become a little confusing since the Name property of the control that is also cCommand1 d. In other word, both the Name and the Caption (what appears on the Command Button control) properties have the same value. To distinguish between them , the Caption on the Command Button control was changed to cClick on Me d, and the Font property was changed too.<br><br> Another note worthy item is that when there are more than one control present in the application during the design time, the properties of the control that is selected will appear in the Properties window. Note the handles around the Command Shahab D. Mohaghegh Chapter ONE -11 Visual Basic for Engineers & Scientists Button control in Figure 1-10.<br><br> These handles are indicators that Command Button control has been selected. Try It! Open Visual Basic and start a Standard.EXE project.<br><br> In the Form that appears place a Command Button control by double clicking on it in the Toolbox. Change the properties of the control by making it larger. Now try to change the Caption on the control, then change the Font.<br><br> Repeat this operation for the Text Box control and the Label control. What If? Write your first program by following these steps: " Open Visual Basic and start a Standard.EXE project.<br><br> " Place a Command Button on the Form. " Change the Caption property to cClick on me to end the Program d. " Using the handles on the control, change its size until the above Caption appears on a single line.<br><br> " Change the Font to Arial (Bold). " Double click on the Command Button control to bring up the code-editing window. " In the cCommand1_Click() d event procedure, between the two provided lines (they read cPrivate Sub Command1_Click() d and cEnd Sub d) type cend d.<br><br> Shahab D. Mohaghegh Chapter ONE -12 Visual Basic for Engineers & Scientists " Then run the program by clicking on the (Play Button) in the Tool bar. First Visual Basic Program In this application you are asked to write a program that only consists of a Form and a Command Button.<br><br> When the application starts it should show the Form with the Command Button in the center of it. The wording on the Command Button should read cClick on Me to End the Program d. When that Button is clicked the application should close.<br><br> 1 Problem Statement Write an application that can be closed (terminated) click of a button. 2 Input/Output Description There are no particular inputs and out puts in this application. There are actions and outcomes instead.<br><br> The action is the click of the Command Button by the user and the outcome should be the termination of the program. 3 Application Development " Open the Visual Basic and choose the Standard.EXE from the opening window. " Double Click on the Command Button control in the Toolbox.<br><br> This will place a Command Button control on the empty Form. The Command Button is now selected on the Form. You can see the handles around it.<br><br> Also, the Properties window is displaying the properties of the Command Button that it has given the Name Command1. Shahab D. Mohaghegh Chapter ONE -13 Visual Basic for Engineers & Scientists " Click on the Caption property in the Property window and change its value from Command1 to cClick on Me to End the Program d.<br><br> " Using the handles of the Command Button increase the size of the Command button so the text can be shown on a single line. " Now click on the form itself to select it. You will notice that the Property window is now displaying the Form properties.<br><br> " Change the Caption property value from Form1 to First Program. " Double click on the Command Button. This will bring up the code-editing window.<br><br> " Make sure that the word cCommand1 d appears in the top left Combo Box and the word cClick d is displayed in the top right Combo Box. First line of this window should read d Private Sub Command1_Click() d and there is an amply line and then cEnd Sub d. In the empty line just type cend d.<br><br> " Close the code-editing window by clicking on the cross (X) on the top right corner of the window. " The code-edit window should look like Figure 1-11. Figure 1-11.<br><br> Code-editing window " Click on the Play Button to run your program. When you program is running, by clicking on the Command Button the program will terminate. Shahab D.<br><br> Mohaghegh Chapter ONE -14 Visual Basic for Engineers & Scientists " Now click on File on the Menu bar, and then click on cSave Project As& d This will bring up a dialog box that asks you to name each form involved in your project and at the end name the entire project. Give your project the proper name (Project 1 for example). " Click on File again and then on cMake Project1.EXE d.<br><br> This will generate an executable file called Project1.EXE, which you can click on to run your program in the Windows operating system environment. The program interface should look like this. Figure 1-12.<br><br> First program interface. The Hello World Program In this application you are asked to write a program that consists of a Form a Label and a Command Button. When the application starts it should show the Form with a large cHello World d written on it in blue.<br><br> The Command Button is for termination of the application. 1 Problem Statement Write an application that displays cHello World d and can be closed by click of a button. Shahab D.<br><br> Mohaghegh Chapter ONE -15 Visual Basic for Engineers & Scientists 2 Input/Output Description There are no particular inputs and out puts in this application. There are actions and outcomes instead. The action is the click of the Command Button by the user and the outcome should be the termination of the program.<br><br> 3 Application Development " Open the Visual Basic and choose the Standard.EXE from the opening window. " Double Click on the Label control in the Toolbox. This will place a Label control on the empty Form.<br><br> The Label is now selected on the form . You can see the handles around it. Also, the Properties window is displaying the properties of the Label that it has given the Name Label1.<br><br> " Click on the Caption property in the Property window and change its value from Label1 to cHello World d. " Click on the Font Property. This will cause a small button with three dots on it to appear on the left of the Font value.<br><br> Click on that button to bring up the Font dialog box. Use the dialog box and choose the font of your choice in bold and size of around 20. Then click on the ForeColor property to change the Font color.<br><br> " Double click on the Command button control in the Tool box to place a Command Button control on the Form " Click on the Caption property in the Property window and change its value from Command1 to c&Exit d. This will cause the word Exit to appear on the Command Button in this Form cE xit d. The fact that E is underlined simply means clicking on this button is the same as clicking <control> E on the keyboard.<br><br> " Use the Font property of the Command Box to change the caption 9s Font. " Now click on the form itself to select it. You will notice that the Property window is now displaying the Form properties.<br><br> Change its caption to cHello World Program d " Double click on the Command button. This will bring up the code-editing window. Shahab D.<br><br> Mohaghegh Chapter ONE -16 Visual Basic for Engineers & Scientists " Make sure that the word cComand1 d appears in the top left Combo Box and the word cClick d is displayed in the top right Combo Box. First line of this window should read dPrivate Sub Command1_Click() d and there is an empty line and then cEnd Sub d. In the empty line just type cend d.<br><br> " Close the Code Editing window by clicking on the cross (X) on the top right corner of the window. " Click on the Play Button to run your program. When you program is running, by clicking on the Command Button the program will terminate.<br><br> " Now click on File on the Menu bar, and then click on cSave Project As& d This will bring up a dialog box that asks you to name each form involved in your project and at the end name the entire project. Give your project the proper name (Project 2 for example). " Click on File again and then on cMake Project2.EXE d.<br><br> This will generate an executable file called Project2.EXE, which you can click on to run your program in the Windows operating system environment. Your interface should look like this. Figure 1-13.<br><br> cHello World d program interface. Shahab D. Mohaghegh Chapter ONE -17 Visual Basic for Engineers & Scientists Summary In this chapter you learned about the Visual Basic development environment.<br><br> You learnd about Menu bar, Tool bar, Toolbox, Project window, Properties window, Layout window, Form window, Code Editing window, and different controls that appear in the Toolbox. You also learned how to write simple Visual Basic applications. Keywords Menu bar Tool bar Toolbox Project window Properties window Control Code Application Exercises 1.<br><br> Write a Visual Basic program (Develop a Visual Basic Application) that displays your name in full and terminates with click of a button. 2. Write a Visual Basic program (Develop a Visual Basic Application) that displays your name, your University 9s Name and your Major.<br><br> This information should appear in three different sizes, colors and fonts. Your program (application) should terminate with click of a button. Shahab D.<br><br> Mohaghegh Chapter ONE -18 Visual Basic for Engineers & Scientists 2 Basic Computations with Visual Basic In this chapter you will be introduced to several basic computation methods using Visual Basic. In this chapter you will learn about variables, data types, operators, two different types of decision structures. You will also be writing more sophisticated Visual Basic programs.<br><br> It is important that you follow the path provided by this manuscript and complete the exercises at the end of the chapter. Introduction In this chapter you will start by writing your first program. This will be a very simple program, and does not require writing any code.<br><br> You will write your first line of code once you try to improve this program. In writing this first program you will use the knowledge that you have acquired during the previous chapter. After writing the first program you will be introduced to other concepts in Visual Basic such as variables, operator, and decision structure.<br><br> Each one of these concepts are introduced in detail and new windows applications (programs) will be developed at each stage. WRITING VISUAL BASIC CODE One of the outstanding features of Visual Basic is that you can accomplish a lot by writing only a few lines of code. In Visual Basic, code is also referred to as program statement.<br><br> A program statement can be as simple as a single word; Shahab D. Mohaghegh Chapter TWO - 19 Visual Basic for Engineers & Scientists Example: Beep This statement causes the computer speaker to make a beep sound. A program statement can also be quite sophisticated.<br><br> One of the most usual forms of the program statements is as follows: Object name. property name = value Example: Label1.Caption = cHello World d This program statement assigns the words cHello World d to the Caption property of an object called Label1. This format is used when you are trying to assign or change a property of an object or control from within a program.<br><br> Many of the properties for each object is usually set at the beginning of a program, not by writing any code, rather by using the properties window as was mentioned in the previous chapter. IMPROVING THE FIRST PROGRAM In the previous chapter you wrote your first program, it is time to use other objects/controls to improve this program. We will use Command Button and Text Box controls to improve the Hello Program.<br><br> The New Hello Program 1. Open a new Visual Basic Project by clicking on the "New Project" on the "File" menu, and selecting Standard.EXE from the list of icons. 2.<br><br> Select "Save As..." from the File menu. A dialog box appears with Form1.frm in the File Name window. Type "New Hello" in place of Form1 and click "O.K." (Make sure you are in the directory that you want).<br><br> Another dialog box appears with Shahab D. Mohaghegh Chapter TWO - 20 Visual Basic for Engineers & Scientists Project1.vbp in the File Name window. Type "New Hello" in place of Project1 and click "O.K." 3.<br><br> Develop the interface using the following information. Object Property Value Form Caption The New Hello Program Name frmHello Text Box Name txtHello Multiline True Alignment Center Text {Delete the Text1} Command Button Caption &Display Name cmdDisplay Command Button Caption &Clear Name cmdClear Command Button Caption &Exit Name cmdExit In order to make the text appear in the center of the Text Box control the Alignment property of the Text Box is changed to ccenter d. This change will only take place if the Multi-line property of the Text Box control is changed to cTrue d.<br><br> Also note that we have added a c& d before letter cD d in the Display Command Box Caption properties. This will cause Visual Basic to execute this command (same as clicking on the Command Button) when the user uses the keyboard (holding the Control button and pushing the letter D). The form will look like Figure 2-1: Shahab D.<br><br> Mohaghegh Chapter TWO - 21 Visual Basic for Engineers & Scientists Figure 2-1. The New Hello Program Interface. 4.<br><br> Attach code to the Interface Now its time to attach some code to the objects. For instance the program should end once the command button exit is clicked. Codes for each object or control is written in that control 9s code-editing window.<br><br> To view and write code in the code-editing window you can either double click on the control or click once and then click on cView Code d in the project window. To attach code to the cE xit d Command Button, double click on this button, you will see the following window: Figure 2-2. Code-editing window for the E xit Command Button.<br><br> Shahab D. Mohaghegh Chapter TWO - 22 Visual Basic for Engineers & Scientists Please notice a few things about this window, which is true for all code windows: " The name of the form where the control is located is printed in the top left corner. This tells you that you are writing the code for this Form.<br><br> " In the object drop-down list (Combo Box), at top left, you will see a list of all controls in this Form. The form (frmHello) contains 4 controls (Exit Command Button, Display Command Button, Clear Command Button, and Text Box). In addition to these controls you can see two more entries, namely, Form and General.<br><br> " This is a complete listing of controls and objects that you can attach code to. " In the procedure drop-down list (Combo Box) at the top right there is a list of available procedures for each object. " Depending on what object and procedure is displayed at these two drop-down lists, two lines of codes is written in the body of the window.<br><br> These lines are not to be interfered with. Visual Basic provides you with starting and ending code lines for each object and its procedure. " Write the desired code for that object and procedure between the provided starting and ending lines.<br><br> In most of the programs that are developed throughout this book, there will be an Exit button that terminates the program. This button is always programmed the same as it appears above. Therefore whenever in this book you see an Exit button, you may assume it simply has an cEnd d in its Click event procedure, unless mentioned otherwise.<br><br> Writing Code for the Display Button " Double click on the Display button. " Type the following statements in the code-editing window. Shahab D.<br><br> Mohaghegh Chapter TWO - 23 Visual Basic for Engineers & Scientists txtHello.Text = cHello....World.... d Figure 2-3 shows the code-editing window for the Display command button after the code has been written. Figure 2-3. Code-editing window for the Display button.<br><br> Please note that three different text colors is used. " Black for the code. " Green for comments, specified by 8 (single quote) " Blue for reserved words.<br><br> These are words used by Visual Basic, such as commands, key words, and functions. With this code you are simply changing the Text property of the ctxtHello d Text Box object to cHello....World... d. This code is executed whenever the Display Command Button is clicked.<br><br> Hence the first line in the Code Window provided by Visual Basic is: Private Sub cmdDisplay_Click() Shahab D. Mohaghegh Chapter TWO - 24 Visual Basic for Engineers & Scientists Which corresponds to cmdDisplay control and Click procedure. Now add the following code to the Clear Command Button.<br><br> txtHello.Text = c d Which means whenever this Command Button is clicked, change the text property of the txtHello Test Box to c d (blank). You have now completed the new and improved Hello program. 5.<br><br> Generate the executable file for this Program by clicking on cFile d in the menu bar and choosing c Make EXE File... d. The c Make EXE File d dialog box appears and you will be asked if you would like to save the executable file in cNew Hello.exe d.<br><br> Accept by clicking c O.K d. Anatomy of an Event Procedure Looking at Figure 2-3 one notices several distinct features. These features are common among all the event procedures.<br><br> Let 9s visit each component of the even procedure in more detail. The first component is the word cPrivate d. This means that this procedure is private to this form and can only be seen by procedures in this form.<br><br> The alternative of a cPrivate d procedure is a cpublic d procedure. A public procedure can be seen and used by all the forms and modules in a project. The second word after cPrivate d is the word cSub d.<br><br> This indicates that this is a sub- procedure as opposed to a function. If it were a function then the second word would be cFunction d instead. A sub-procedure in Visual Basic is equivalent to a sub-program or a subroutine in other high level programming language.<br><br> Shahab D. Mohaghegh Chapter TWO - 25 Visual Basic for Engineers & Scientists Next in Figure 2-3 we see the term ccmdDisplay_Click() d. This is the name of the procedure.<br><br> Every procedure within a form must have a unique name. It includes three parts. First is the name of the control (cmdDisplay in this case) followed by a cunder score d character.<br><br> The second part immediately after the under score character is the event. In Figure 2-3 the click event is identified. The last part is the empty parentheses ().<br><br> This means that the procedure does not expect to receive any information from the procedure or event that calls it. Such information is usually in the form of parameters or arguments that are passed to the event procedure. The last component in the Figure 2-3 is the word cEnd Sub d.<br><br> This indicates that the procedure has reached its end. Try It! Writing a program in Visual Basic that displays different information about you when different Command Buttons are clicked.<br><br> You may start with Name, Last Name, Address, and Telephone Number. Make sure your program contains a Clear Command Button. VISUAL BASIC VARIABLES AND OPERATORS Variables are temporary memory locations that can contain different types of data.<br><br> In computer programming data is stored in variables for later retrieval. In Visual Basic variables are also used to transfer information to an object property. When naming variables in your Visual Basic programs you should adhere to the following conventions: Shahab D.<br><br> Mohaghegh Chapter TWO - 26 Visual Basic for Engineers & Scientists Keep it as short as possible without making it too cryptic. It should be intuitive and descriptive. It should start with a letter.<br><br> There should be no periods in the variable name. It should be less than 256 characters long. You may use upper and lower case characters (Visual Basic is not case sensitive).<br><br> You may use numbers. You should not use reserved names such as Visual Basic Keywords, Objects or properties. DATA TYPES It is a common practice in computer programming to declare the type of the variable that is being used.<br><br> Variable type refers to the type of data that the variable is hosting. Following is a list of data types used in Visual Basic: Data Type Size Range Integer 2 bytes -32,768 Through 32,767 Long Integer 4 bytes -2,147,483,648 through 2,147,483,648 Single (precision floating point) 4 bytes -3.042823x10 38 through 3.042823x10 38 Double (precision floating point) 8 bytes -1.79769313486232x10 308 through 1.79769313486232x10 308 Currency 8 bytes -922,337,203,685,477.5808 through 922,337,203,685,277.5808 String 1 byte/character 0 through 65,535 characters Shahab D. Mohaghegh Chapter TWO - 27 Visual Basic for Engineers & Scientists Boolean 2 bytes true/false Date 8 bytes January 1,100 through December 31,9999 Variant 16 bytes(number) All data type range 22 bytes+1 byte/Character(string) Table 2-1.<br><br> Data types in Visual Basic. When you do not specify the type of a variable in a declaration statement it takes the default type which is a variant. As you can see in the above table, variant type allows the variable to take both text (string) and number.<br><br> Variables with variant data type can store all the data types mentioned above and switch format automatically. It is highly recommended that you use specific data types for specific tasks and variables. For example a counter should always be an integer.<br><br> Declaring variables at the beginning of each program emphasizes discipline, and saves memory and increases program efficiency. As you can see from the above table using variant data types, although convenient, is not the most efficient way of programming. VARIABLE DECLARATION There are two ways to declare variables in Visual Basic.<br><br> cImplicit d declaration is when you start using a variable simply by assigning a specific data type to it. cExplicit d declaration is when you declare each variable at the beginning of the program. Using cOption Explicit d statement in the general declaration section of a program forces Visual Basic to generate an error message if a variable is not declared.<br><br> This way you make sure that you are using only explicit form of variable declaration. To declare a variable as a specific data type use the following format: Shahab D. Mohaghegh Chapter TWO - 28 Visual Basic for Engineers & Scientists Dim Counter As Integer Dim Velocity As Single Dim Distance As Single Dim LastName As String Local and Global Variables If a variable is declared in a specific event procedure, i.e.<br><br> cmdDisplay_Click(), then it is a local variable and can only be used in that event procedure with the declared format. If you want to use this variable in another event procedure, then you must declare it again. In order to make a variable available to all the controls and procedures in your program, you need to declare it as a global variable.<br><br> This is done by declaring the variable in the General (declaration) event procedure. This event procedure is available through the Code Window as the first item in the cObject d drop-down list and first item in the cProc: d drop-down list as seen in the following figure. Figure 2-4.<br><br> The Code Editing Window. Constants Constants are numbers that their value does not change throughout your project. In many engineering problems there are constant numbers that are used quite often.<br><br> In Visual Shahab D. Mohaghegh Chapter TWO - 29 Visual Basic for Engineers & Scientists Basic you can declare a constant by using the keyword "Const". One of the more familiar constants is À .<br><br> To declare À as a constant in Visual Basic use the following format. Const pi= 3.14159265 OPERATORS IN VISUAL BASIC Mathematical operations in Visual Basic take place by the use of operators. Operators will help us add, subtract, multiply, divide and exponentiate numerical values.<br><br> Following is a list of mathematical operations used in Visual Basic. Operator Meaning Example ^ Exponentiation 3 ^3=27 * Multiplication 3 * 3=9 / Division 3 / 3=1 \ Integer Division 7 \ 2=3 - Subtraction 3 - 3=0 + Addition 3+3=6 Mod Remainder Division 9 Mod 2=1 Table 2-2. Operators in Visual Basic.<br><br> The Operators Program Now lets try writing a Visual Basic program to examine each of the operations mentioned above. 1. Open a new Visual Basic Project by clicking on the "New Project" on the "File" menu, and selecting Standard.EXE from the list of icons.<br><br> Shahab D. Mohaghegh Chapter TWO - 30 Visual Basic for Engineers & Scientists 2. Select "Save As..." from the File menu.<br><br> A dialog box appears with Form1.frm in the File Name window. Type "Operators" in place of Form1 and click "O.K." (Make sure you are in the directory that you want). Another dialog box appears with Project1.vbp in the File Name window.<br><br> Type "Operators" in place of Project1 and click "O.K." 3. Develop the interface using the following information. Object Property Value Form Name frmOperators Caption The Operators Program Text Boxe Name txtNumber1 Multilin True Alignment Center Text Box Name txtNumber2 Multiline True Alignment Center Text Box Name txtResult Multiline True Alignment Center Forecolor Red Label Caption First Number Label Caption Second Number Label Caption Result Command Button Name cmdExit Caption &Exit Command Button Name cmdAdd Caption &Add Shahab D.<br><br> Mohaghegh Chapter TWO - 31 Visual Basic for Engineers & Scientists Command Button Name cmdSubtract Caption &Subtract Command Button Name cmdMultiply Caption &Multiply Command Button Name cmdDivide Caption &Divide Command Button Name cmdInteger Caption &IntegerDivide Your program 9s interface should look like this: Figure 2-5. The Operators program interface. 4.<br><br> Attach code to the Interface Declarations of variables Click on the "View Code" in the Project Window In the "Object"drop-down list select "General" and in the "Proc:"drop-down list select c(declarations) d Type the following codes in this window Shahab D. Mohaghegh Chapter TWO - 32 Visual Basic for Engineers & Scientists Dim x As Single Dim y As Single Dim z As Single Or you may type: Dim x, y, z As Single Your Code Window should look like this: Figure 2-6. Variable declaration in Visual Basic.<br><br> Addition f Double click on the Add Command Button f Visual Basic responds by opening the Code Window f Type the following lines in the Code Window x = Val (txtNumber1.Text) y = Val (txtNumber2.Text) z = x + y txtResult.Text = z Shahab D. Mohaghegh Chapter TWO - 33 Visual Basic for Engineers & Scientists Figure 2-7. Code Editing window for cmdAdd Command button.<br><br> Subtraction f Double click on the Subtract Command Button f Write the following code in cmdSubtract Code Window x = Val (txtNumber1.Text) y = Val (txtNumber2.Text) z = x - y txtResult.Text = z As you can see this code is identical to the code that was typed in the cmdAdd_Click() event procedure. The only difference is that the value assigned to variable z is the subtraction of x and y and not the addition. Other Operations f Do the same as you did for the add and the subtract Command Buttons for the other Command Buttons namely, Multiply, Divide and Integer Divide.<br><br> Only change the operator for each Command Button as applicable. Shahab D. Mohaghegh Chapter TWO - 34 Visual Basic for Engineers & Scientists 5.<br><br> Generate the executable file for this Program by clicking on cFile d in the menu bar and choosing c Make EXE File... d. The c Make EXE File d dialog box appears and you will be asked if you would like to save the executable file in c Operators.exe d.<br><br> Accept by clicking c O.K d. Code Interpretation In the first line you are assigning the user input or the value of the Text property of the txtNumber1 Text Box to the variable x. While the program is running, user will type the first number for the operation in this Text Box.<br><br> Your code will instruct Visual Basic to retrieve this value and assign it to the Text property of the txtNumber1 object. The same is true for txtNumber2 object in the second line of the code . Then the two variables x and y are added together and the result is assigned to the variable z.<br><br> The variable z is then assigned to the Text property of the txtResult object that will consequently be displayed in the interface. OPERATOR COMBINATION AND MATHEMATICAL FORMULAS Combining the mathematical operators that were discussed above carries out many engineering calculations. By combining mathematical operators with mathematical functions (that will be covered in this section), you can handle majority of the engineering calculations.<br><br> These combinations are called mathematical formulas that describe certain behaviors. For example following mathematical formula describes the distance of the center of gravity from a reference plane in a hollow cylinder sector: ( ) () a a C s r s r 2 2 3 3 sin 1972 . 38 2 2 = Shahab D.<br><br> Mohaghegh Chapter TWO - 35 Visual Basic for Engineers & Scientists Operator Precedence A mathematical formula processes a combination of variables and operators. Following is a valid Visual Basic program statement: X = 12 + 8 / 4 - 1 * 3^2 Above calculation may have several different results depending on which operator is executed first. For example is 12 added to 8 and the result is then divided by 4 or 9 is first divided by 4 and the result is added to 12?<br><br> Visual Basic executes mathematical operators in a specific order. Following is the order of precedence that operators are executed in Visual Basic. Operator Order of Precedence ^ First * and / Second \ Third Mod Forth + and - Fifth Table 2-3.<br><br> Operators order of precedence in Visual Basic. In the above expression 3^2 is executed first. Then 9 (the result of 3^2) is multiplied by 1 and 8 is divided by 4.<br><br> The above expression reduces to: X = 12 + 2 - 9 Which will add up to 5. Shahab D. Mohaghegh Chapter TWO - 36 Visual Basic for Engineers & Scientists When writing formulas in Visual Basic, it is recommended to use parentheses to separate calculations.<br><br> In Visual Basic, parentheses take precedence over everything else. Innermost parentheses are always executed first regardless of the operators they contain. Therefore use of parentheses forces precedence in calculations.<br><br> Consider the following expression: Y = 3 + 5^2 / 9 * 2^3 3 10 If we present this expression in this format to Visual Basic the result will be 15.2222, but this expression should resemble the following formula: 10 2 9 5 3 3 2 2 × + = Y Above expression has a value of 0.4516. To make sure that the formula is presented to the Visual Basic correctly, we must use parentheses. So the expression should be written as: Y = (3 + 5^2) / (9 * 2^3 - 10) which will result in the correct answer.<br><br> Shahab D. Mohaghegh Chapter TWO - 37 Visual Basic for Engineers & Scientists Mathematical Functions For more number crunching power, Visual Basic provides a set of mathematical functions. Functions operate on a variable, an expression or a value that is provided as an argument (n).<br><br> Following is a list of some of the Visual Basic functions. Function Purpose Sin(n) Returns the sine of the angle n. the angle is expressed in radians Cos(n) Returns the cosine of the angle n.<br><br> the angle is expressed in radians Tan(n) Returns the tangent of the angle n. the angle is expressed in radians Atn(n) Returns the arctangent of n, in radians Abs(n) Returns the absolute value of n Exp(n) Returns the constant e to the power n Rnd(n) Generates a random number between 0 and 1 Sgn(n) Returns -1 if n is less than zero, 0 if n is zero, and +1 if n is greater than zero Sqrt(n) Returns the square root of n Str(n) Converts a numeric value to a string Val(n) Converts a string value to a number Table 2-4. Pre-defined functions in Visual Basic.<br><br> You will find that majority of the mathematical formulas in engineering are a combination of the above functions and mathematical operators. We will now write a program that uses some mathematical operators as well as mathematical functions. Shahab D.<br><br> Mohaghegh Chapter TWO - 38 Visual Basic for Engineers & Scientists The Function Program Now we will write an application that allows user to change the angle from 0 to 360 degrees using a sliding horizontal scroll bar. In 4 different Text Boxes user will see the current value of the angle, as well as the sine, cosine and the tangent of that angle. 1.<br><br> Open a new Visual Basic Project by clicking on the "New Project" on the "File" menu, and selecting Standard.EXE from the list of icons. 2. Select "Save As..." from the File menu.<br><br> A dialog box appears with Form1.frm in the File Name window. Type "Functions" in place of Form1 and click "O.K." (Make sure you are in the directory that you want). Another dialog box appears with Project1.vbp in the File Name window.<br><br> Type " Functions" in place of Project1 and click "O.K." 3. Develop the interface using the following information. Object Property Value Form Name frmFunctions Caption The Functions Program Text Box Name txtAngle Multiline True Alignment Center Text Box Name txtSine Multiline True Alignment Center Text Box Name txtCosine Multiline True Alignment Center Text Box Name txtTan Shahab D.<br><br> Mohaghegh Chapter TWO - 39 Visual Basic for Engineers & Scientists Multiline True Alignment Center Label Caption ANGLES in Degrres Label Caption Sine Label Caption Cosine Label Caption Tangent Label Caption 0 Label Caption 360 Label Caption Angles in Degrees Command Button Name cmdExit Caption &Exit Horizontal Scroll Bar Name hsbFunctions Min 0 Max 360 Value 180 Your program 9s interface should look like this: Figure 2-8. The Functions Program Interface. Shahab D.<br><br> Mohaghegh Chapter TWO - 40 Visual Basic for Engineers & Scientists In the Horizontal Scroll Bar control, we have changed the settings on three properties other than the Name property. These are Min, Max and Value properties. Min and Max properties set the lower and upper limits of the scroll bar and Value property determines the initial location of the sliding thumb.<br><br> You may use the Font and ForeColor properties in the Label and the Text Box controls to change the typeface, size, and the color of the fonts. 4. Attach code to the Interface Declaration of variables f Click on the "View Code" in the Project Window f In the "Object"drop-down list select "General" and in the "Proc:" drop- down list select c(declarations) d f Type the following codes in this window 'Declare Variables and Constants Dim x As Single Dim y As Single Dim w As Single Dim z As Single Const pi = 3.1416 f Write the code for the Horizontal Scroll Bar control f Double click on the Horizontal Scroll Bar control to bring up its Code Window.<br><br> f Write the following code in the hsbFunctions_Change() event procedure. Shahab D. Mohaghegh Chapter TWO - 41 Visual Basic for Engineers & Scientists 'Read The angle from the Scroll Bar and x = hsbFunctions.Value txtAngle.Text = x 'Change it to radians a = x * pi / 180 'Calculate the Functions and show them y = Sin(a) txtSine.Text = y w = Cos(a) txtCosine.Text = w z = Tan(a) txtTan.Text = z 5.<br><br> Generate the executable file for this Program by clicking on cFile d in the menu bar and choosing c Make EXE File... d. The c Make EXE File d dialog box appears and you will be asked if you would like to save the executable file in c Functions.exe d.<br><br> Accept by clicking c O.K d. Code Interpretation In the first line you are assigning the current value of the scroll bar to the variable x. In the second line value of x is displayed in the txtAngle Text Box.<br><br> Then the angle is changed to radians, since the functions we will be using will need their arguments to be in radians. In the next six lines we first calculate the sine, cosine and tangent of the angle and then they are displayed in the proper Text Box control. Shahab D.<br><br> Mohaghegh Chapter TWO - 42 Visual Basic for Engineers & Scientists OTHER OPERATORS Visual basic has other operators beside mathematical operators that were just covered. These are called comparison operators and logical operators. Next section will be devoted to these kinds of operators.<br><br> Try It! Write a program that accepts user 9s inputs in Centimeters and provides inches, feet and meters as output. VISUAL BASIC DECISION STRUCTURES Visual Basic allows you to add logic to your program as well as to control the flow of your program.<br><br> Using the decision structure you can evaluate any given situation and take the appropriate action. In other words Visual Basic 9s decision structure allows you to examine a condition and use the outcome of the condition to decide which program statements to execute next. You can do this in Visual Basic using two different types of decision structures.<br><br> If...Then decision structure and Select Case decision structure. IF ... THEN DECISION STRUCTURE Using comparison operators within an If ...<br><br> Then decision structure you can control the order in which the program statements are executed. An If ... Then decision structure has one of the following formats: If condition Then statement or If condition Then Shahab D.<br><br> Mohaghegh Chapter TWO - 43 Visual Basic for Engineers & Scientists statement1 statement2 End If or If condition Then statement1 Else statement2 End If or If Condition1 Then Statement1 Statement2 ElseIf Condition2 Then Statement3 Statement4 ElseIf Condition3 Then Statement5 Statement6 End If As you see the If ... Then decision structure can have several formats. You should use the comparison operators to evaluate the conditions such as cGreater Than d, cLess Than d, and others.<br><br> Following table shows Visual Basic 9s comparison operators. Shahab D. Mohaghegh Chapter TWO - 44 Visual Basic for Engineers & Scientists Comparison Operator Meaning = Equal to <> Not Equal to > Greater Than < Less Than >= Greater Than or Equal to <= Less Than or Equal to Table 2-5.<br><br> Comparison operators in Visual Basic. Example: If Option1.Value= True Then Label1.Caption= dFirst Option Is Selected d or Dim TempFahrenheit As Single , TempCelsius As Single If TempCelsius<>100.0 Then TempFahrenheit=(9.0/5.0)*TempCelsius+32 Text1.Text=TempFahrenheit End If or Dim interval As Integer , Counter As Integer If interval<100 Then Counter=Counter+1 Else Counter=Counter+2 End If Shahab D. Mohaghegh Chapter TWO - 45 Visual Basic for Engineers & Scientists or Dim Temperature As Single If Temperature<50 Then Text1.Text= cThe Temperature is too Low d ElseIf Temperature>50 and Temperature<100 Then Text1.Text= Temperature ElseIf Temperature>100 Then Text1.Text= cThe Temperature is too High d End If As you probably have noticed there are times that you have to combine more than one condition to make a new condition.<br><br> In the last example the second cElseIf d condition states that cIf Temperature is between 50 and 100 Then ... d which needs more than one condition to be met. In such cases you should use logical operators. Following is a list of logical operators and their meaning in Visual Basic.<br><br> Logical Operator Meaning And If both conditional expressions are True, then the result is True. Or If either conditional expressions are True, then the result is True. Not If the conditional ex p ressions is False , then Shahab D.<br><br> Mohaghegh Chapter TWO - 46 Visual Basic for Engineers & Scientists the result is True. If the conditional expressions is True, then the result is False. Xor (Exclusive Or) If one and only one of the conditional expressions is True, Then the result is True.<br><br> If both are True, or if both are False, then the result is False. Table 2-6. Logical operators in Visual Basic.<br><br> Following table displays the result of combination of comparison and logical operators in a compact form: A B Not A A or B A and B False False True False False False True True True False True False False True False True True False True True Table 2-7. Combination of logical operators in Visual Basic. Now lets use the if ...<br><br> Then decision structure in a program. The New Operators Program This program is a different version of the program that was developed in the last section. Now instead of using a separate command button for mathematical operations we will try to implement the same idea through the If ...<br><br> Then decision structure and use of the Frame and the Option Button objects or controls. Shahab D. Mohaghegh Chapter TWO - 47 Visual Basic for Engineers & Scientists 1.<br><br> Open a new Visual Basic Project by clicking on the "New Project" on the "File" menu, and selecting Standard.EXE from the list of icons. 2. Select "Save As..." from the File menu.<br><br> A dialog box appears with Form1.frm in the File Name window. Type "New Operators" in place of Form1 and click "O.K." (Make sure you are in the directory that you want). Another dialog box appears with Project1.vbp in the File Name window.<br><br> Type "New Operators" in place of Project1 and click "O.K." 3. Develop the interface using the following information. Object Property Value Form Name frmNewOperators Caption The New Operators Program Text Box Name txtNumber1 Multiline True Alignment Center Text Box Name txtNumber2 Multiline True Alignment Center Text Box Name txtResult Multiline True Alignment Center Forecolor Red Label Caption First Number Label Caption Second Number Label Caption Result Command Button Name cmdExit Caption &Exit Shahab D.<br><br> Mohaghegh Chapter TWO - 48 Visual Basic for Engineers & Scientists Command Button Name cmdCalculate Caption &Calculate Frame Name frmNewOpt Caption Pick an Operator Option Button Name OptAdd Caption Add (+) Value True Option Button Name OptSubtract Caption Subtract (-) Option Button Name OptMultiply Caption Multiply (*) Option Button Name OptDivide Caption Divide (/) Option Button Name OptInteger Caption Integer Division (\) Option Button Name OptExponent Caption Exponentioation (^) At this point few notes on the use of Option Buttons and Frames should be mentioned. Usually, Option Button controls are used in a group of options from which the user selects only one. In this problem all mathematical operators are grouped together.<br><br> You group Option Button controls by drawing them inside a container such as a Frame control. To group Option Button controls in a Frame, draw the Frame first, and then draw the Option Button controls inside the Frame control. All Option Button controls within the same container act as a single group.<br><br> Shahab D. Mohaghegh Chapter TWO - 49 Visual Basic for Engineers & Scientists When a user selects an Option Button or a mathematical operation, the other Option Button controls or mathematical operations in the same group are automatically unavailable. Also making the Value property of the OptAdd Option Button to be True, this Option Button will become the default choice in the group.<br><br> The interface that you just developed should look like this: Figure 2-9. The Enhanced Operators program interface. 4.<br><br> Attach code to the Interface Declarations of variables f Click on the "View Code" in the Project Window f In the "Object"drop-down list select "General" and in the "Proc:"drop-down list select c(declarations) d f Type the following codes in this window Dim x As Single , y As Single , z As Single Calculate Command Button Shahab D. Mohaghegh Chapter TWO - 50 Visual Basic for Engineers & Scientists f Double click on the Calculate Command Button f Visual Basic responds by opening the Code Window f Type the following lines in the Code Window 'Read in the first and the second numbers x = Val (txtNumber1.Text) y = Val (txtNumber2.Text) 'Find which option is selected and then take appropriate action If OptAdd.Value = True Then z = x + y ElseIf OptSubtract.Value = True Then z = x - y ElseIf OptMultiply.Value = True Then z = x * y ElseIf OptDivide.Value = True Then z = x / y ElseIf OptInteger.Value = True Then z = x \ y ElseIf OptExponent.Value = True Then z = x ^ y End If 'Write the Result in the txtResult Text Box txtResult.Text = z Shahab D. Mohaghegh Chapter TWO - 51 Visual Basic for Engineers & Scientists 5.<br><br> Generate the executable file for this Program by clicking on cFile d in the menu bar and choosing c Make EXE File... d. The c Make EXE File d dialog box appears and you will be asked if you would like to save the executable file in c NewOperators.exe d.<br><br> Accept by clicking c O.K d. Code Interpretation As in the previous program, in the first two lines you are assigning the values of the Text property of the txtNumber1 and txtNumber2 (user input) to the variables x and y. The If ...<br><br> Then decision structure will identify which mathematical operation should be executed by examining the Value property of all the six Option Buttons available. The Option Button with a True Value property, which will be the option selected by the user at run time, will be executed and the result of the operation is assigned to the variable z. The variable z is then assigned to the Text property of the txtResult object that will consequently be displayed in the interface.<br><br> SELECT CASE DECISION STRUCTURE Flow of a program can also be controlled using the Select Case decision structure. Select Case decision structure lets you execute one of the several statements or group of statements. Select Case decision structure has the following format: Select Case Variable Case value1 Statement1 Statement2 Case valu<br><br>