ebook img

Introduction To Computer Programming And Numerical Methods PDF

344 Pages·2007·4.682 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Introduction To Computer Programming And Numerical Methods

Chapter 1 Xudong Jia & Shu Liu Chapter 1 Visual Basic.Net Environment Welcome to Introduction to Computer Programming and Numerical Methods. The objective of this chapter is to introduce Microsoft Visual Basic.Net (or VB.Net), the most useful computer programming language. You will learn step-by-step the Integrated Development Environment (IDE) inside VB.Net for creating, running and debugging VB.Net programs. 1.1 Getting Started Assume you have a user account and a password for use of computers in a university or college lab. Now it is the time for you to enter the user account name and the password into a lab computer. [If you have a home computer where VB.Net and Windows XP is installed on it, you can also learn the VB.Net Integrated Development Environment using your home computer.] The procedures described in this chapter apply both your lab and home computers. After you log on to a computer (lab or home), you are in the Windows XP operating system. You are encouraged to change the password to a new one for security reasons. Otherwise, someone may break into your account and delete your files accidentally or with some purposes. The steps to change your password and start VB.Net are as follows: (cid:1) Press Alt+Del+Ctrl keys at the same time and follow the instructions provided in the window dialog to change your password. (cid:1) Click the Start button at the bottom of the Desktop using the left mouse button. (cid:1) Select Programs-> Microsoft Visual Studio .Net 2003 -> Microsoft Visual Studio .Net 2003. You will be in the .Net development environment (see Figure 1-1). It is noted that the Visual Studio .Net 2003 version is used in this book. All the programs listed in this book are tested under this version and saved in the CD at the back of this book. (cid:1) Inside the .Net development environment, you will see three tabs listed in the middle of the environment. These three tabs are Projects, Online Resources, and My Profile. Click the Projects tab and New Project button. (cid:1) Highlight Visual Basic Projects in the Project Type window and Windows Application in the Templates window. Introduction to Computer Programming & Numerical Methods 1 Chapter 1 Xudong Jia & Shu Liu (cid:1) Specify a name called Lab1 and a location of the VB application to be developed. The location you specify is the place where your VB.Net project data and code is located. Figure 1-1 VB.NET Development Environnent (cid:1) Click OK. Now you are in the Visual Basic.Net Integrated Development Environment (IDE). 1.2 VB.Net Integrated Development Environment The integrated development environment (IDE) is important in helping you create, run and debug VB.Net programs or applications. You can consider VB.Net IDE as Microsoft Word and VB.Net programs as Word documents. A Word document is created in Microsoft Word, while a VB.Net program is created in the VB.Net IDE. The VB.Net IDE consists of a set of Graphical User Interfaces (GUIs) and components (see Figure 1-2). You need to understand this IDE because all VB.Net programs or applications are developed within this environment. Let us look at this environment in detail. On the top of the window, you can see the following components: Introduction to Computer Programming & Numerical Methods 2 Chapter 1 Xudong Jia & Shu Liu Figure 1-2 VB.Net Integrated Development Environment Title Bar It shows the title of the VB.Net project you are currently working on. The default project title is the project name you have specified when you create a new project. If you would like to change the project name or title to other name, you can change it through Project -> Project Properties. Menu Bar It consists of menus that help you manipulate VB.Net programs in the project. The menus are listed from left to right as File, Edit, View, Project, Build, Debug, Tools, Window, and Help. Tool Bar Two built-in tool bars are provided when you open the VB.Net IDE. You can add more toolbars. The steps to do that are Select Views->Toolbars Check on the names of the toolbars. In addition to the above components, you should be aware of the five windows in the IDE when you are developing a VB.Net project or application. These five windows are as follows: Toolbox window The window is very important in the VB.Net IDE. It contains control templates or components that are available for you to use. Introduction to Computer Programming & Numerical Methods 3 Chapter 1 Xudong Jia & Shu Liu These components are similar to the engine and transmission in a car. They are well built. Just as people do not need to know how to build an engine or a transmission to assemble a car, you do not need to know how to build a component or a control template in order to develop a VB.Net application. Your job is just to know how to use control templates for a VB.Net program. How to use these control templates? The solution is “drag and drop”. You need to first click on a control template, for example, a label control template and then move the control template into the form window while pressing and holding the mouse. Once you drag and drop an instance of an control template, a control that inherits the behavior of the control template is created. As you can see, the IDE is a VB.Net assembling line that allows you to take components available in the toolbox to make an application. Similar to a car assembling line that allows car builders to select various engines and transmissions to make different cars, the IDE can allow you to access millions of control components that are available through Microsoft VB.Net built-in libraries, third-party software, and user-defined libraries. Form Window The window is a blank window (or form) on which you can place controls. The form and its controls constitute the graphical user interface (GUI) of a VB.Net program. They are the visual part of the program with which the user can interact. Solution Explorer The window contains a Windows Explorer-like tree view of all the customizable forms and general code (modules) that make up a VB.Net application. The Solution Explorer provides you with an organized view of your project and program files associated with the project. A toolbar associated with this window lists a set of commands that allow you to see the graphical user interface (GUI) of your project and to see the code that supports the controls on the GUI. Select the Solution Explorer on the View menu when you cannot find the Solution Explorer in your IDE. Properties Window The properties window displays the properties for a form or a control. Properties describe attributes such as size, color, and font of a control. Each form or control has its own set of properties. Introduction to Computer Programming & Numerical Methods 4 Chapter 1 Xudong Jia & Shu Liu When you click on a control or the form, the properties will be listed in the properties window. There are two columns in the properties window. The first column lists the property names and the second column shows the current value of the property. The value can be changed at the design phase of the form or through the program code. There are lots of properties associated with controls. You do not need to know all of them but understand how to use primary properties such as name, text, etc. 1.3 A Simple VB.Net Application Creating a simple application is a process that implements a logic used in solving a problem. It involves the steps to create forms (or Graphic User Interfaces) and to write code to support the forms. Each form contains a set of controls “dragged and dropped” from control templates. Each control has a set of properties and a group of events or actions such as click, double-click, mouse-up, and key-press. Each event is supported by a sequence of VB.Net code to tell the computer what to do when an event of a control is trigged or activated. Example 1.1 A Simple VB.Net Application Suppose you have a command button control in the form window. The name of the control is called aCommandButton. You can go to the code window through View - >Code, select the aCommandButton control from the control list, and select the click event from the event list. After the operations, you can see the procedure template as follows: Private Sub ACommandButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles _ ACommandButton.Click End Sub Note that there are two arguments inside the parentheses. We will discuss these two arguments later. Now you just understand two arguments are important in this event procedure. When you compare the above procedure to that created by your IDE, you see the above procedure has two “_” letters inserted between the two parentheses. Each “_” letter indicates that the line after the letter is the continuation of its previous line. For example, the first “_” letter indicates that “ByVal e As System.EventArgs) Handles” is considered as part of the first line. With the use of the two “_” letters, the first three lines are considered as one line in VB.Net IDE. Introduction to Computer Programming & Numerical Methods 5 Chapter 1 Xudong Jia & Shu Liu Type the following code between Private Sub and End Sub. Msgbox “Welcome to VB.Net” The entire code looks like: Private Sub ACommandButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles _ ACommandButton.Click Msgbox “Welcome to VB.Net” End Sub Provided that the code has been written for the click event of the aCommandButton control, let us run the project and see what happens. You can find this simple application as Example 1_1 in Chapter 1 of CD. (cid:1) Select Debug -> Start A window that represents the form you just have designed is prompted. Click the command button control and see what the program will do for you. Surprisingly, you will see another window. The window shows the message “Welcome to VB.Net.” This message is created by the VB.Net statement Msgbox “Welcome to VB.Net.” More discussions of VB.Net statements will be provided in later chapters. From the above simple application, we can conclude that the VB.Net IDE takes the following steps to run a VB.Net program: Step 1: Visual Basic.Net monitors the forms and the controls in each form for all the possible events such as mouse movements, clicks, keystrokes and so on. Step 2: When VB.Net detects an event, it searches for a built-in response to the event. If there are not any built-in responses to the event, the VB.Net examines the program to see if an event procedure has been written for that event. If an event procedure is written for that event, VB.Net executes the procedure. VB.Net statements within the procedure will be executed statement by statement from top to bottom. Once all the statements are executed, the program will go back to Step 1. If an event procedure is not written for that event, VB.Net skips this event and waits for the next event and goes back to Step 1. Introduction to Computer Programming & Numerical Methods 6 Chapter 1 Xudong Jia & Shu Liu 1.4 VB.Net Application Development Process A VB.Net application development process involves five major steps, that is, 1) understand the logic needed to solve the problem by a VB.Net program, 2) design form(s) to implement the logic, 3) write program code to support the controls in the form(s), 4) debug the program, and 5) deliver the program to users. Example 1.2 Quadratic Equation VB. Net Application Let us use an example to describe the development process. Suppose we need to develop a VB.Net program to solve the following quadratic equation and find two real roots if they exist: f(x) = ax2 + bx + c = 0 Where a „ 0 and b „ 0 Two logical flows can be developed for the program. These two flows are: Logical Flow #1: A user is prompted a window on which three input boxes for parameters a, b, and c are listed. The user then enters a numerical value in each of these input boxes and clicks a button called Calculate. The program will find two real roots if they exist. The user terminates the execution of the program by clicking on a button called Cancel. We name this logical flow the simple flow. Logical Flow #2: A user is asked to enter a full quadratic function (like 1x^2+2x+1) into an input box. Once the user presses the Enter key, the three parameters (a, b, and c) will be parsed (or extracted) out from the function expression and displayed in the input boxes. The user then clicks a button called Calculate. The program will find two real roots if they exist. The user terminates the execution of the program by clicking on a button called Cancel. We name this logical flow the complex flow. These two logic flows will make the development of a VB.Net application different. Here we only concentrate on the VB.Net implementation of the simple logic and leave the complex logic in later chapters. Example1_2 within Chapter 1 of the CD is the VB.Net application for the simple logic. The development process to implement the simple logic flow involves the following steps: Introduction to Computer Programming & Numerical Methods 7 Chapter 1 Xudong Jia & Shu Liu 1) Form design 2) Program coding 3) Debugging and testing 4) Program packaging and delivery Let us go through these four basic steps: Step 1: Form Design The form design is a process that implements the logic for the program. For the simple logic flow, we need to develop the form as shown in Figure 1-3. Within this form, you can find a form control, five label controls, five text box controls, and two command controls. SimpleLogic Val_a Root1 Val_b Root2 Val_c Calculate Cancel Figure 1-3 Form Designed for the Simple Logic Flow These controls are defined with the following names: Form Control: SimpleLogic Textbox for a: val_a Textbox for b: val_b Textbox for c: val_c Textbox for root1: root1 Textbox for root2: root2 Calculate button: calculate Cancel button: cancel Label for a: Label1 Label for b: Label2 Introduction to Computer Programming & Numerical Methods 8 Chapter 1 Xudong Jia & Shu Liu Label for c: Label3 Label for root1: Label4 Label for root2: label5 These names must follow the VB.Net rules and conventions listed below: 1) Each control must have a unique name that can be no more than 40 characters long. 2) Each control name must start with a letter. The remaining characters can be any letters, numerical characters, or underscores. You cannot have spaces, special symbols, or punctuation characters in a control name. 3) It is a good practice to name a control to reflect the logic of the application. For example, we define Val_a, Val_b, and Val_c to represent the three parameters of a quadratic equation. The procedures to create these controls are as follows: 1) Within the VB.IDE, click the empty form and change its name to be SimpleLogic (see Figure 1-4). Also replace Form1 in the Text property by Simple Logic Application. 2) Highlight the Textbox template within the Toolbox window. Press and hold the cursor and drag the cursor into the Form window. Release the cursor. You will see a textbox control is created (see Figure 1-5). Figure 1-4 Form Window for Example1_2 Introduction to Computer Programming & Numerical Methods 9 Chapter 1 Xudong Jia & Shu Liu Figure 1-5 Create a Textbox Control 3) Note that the name of the textbox control created in 2) is Textbox1. Change it to Val_a. Also Clear the Text property. 4) Follow the same way to create other control names. Note that you do not need to change the label control names since they are not used in the program code. Step 2: Program Coding There are two event procedures to be developed. These two procedures will respond to two click actions (or events), one on the Calculate button and the other on the Cancel button. Public Class quadradic_equation Inherits System.Windows.Forms.Form Windows Form Designer Generated Code Private Sub cancel_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cancel.Click End End Sub Introduction to Computer Programming & Numerical Methods 10

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.