1 / 30

Topic 9: Writing Simple Programs

Topic 9: Writing Simple Programs. Learning outcomes. By the end of this topic, you should be able to: 1. Identify the types of data that can be used by VB and how to use them ; 2. Describe the use of variables and how to manipulate them; 3. State the arithmetic functions contained in VB;

kalkin
Download Presentation

Topic 9: Writing Simple Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topic 9: Writing SimplePrograms

  2. Learning outcomes • By the end of this topic, you should be able to: 1. Identify the types of data that can be used by VB and how to use them; 2. Describe the use of variables and how to manipulate them; 3. State the arithmetic functions contained in VB; 4. Add and control text guided forms; and 5. Write a simple program using several forms

  3. Data Types • Just like other programs, Visual Basic has its own data. The most important type of data is variables. Variable in Visual Basic can receive different types of data such as integers, float, string, char, etc. Following table classify several data types that can be used for variables declaration.

  4. Choosing the appropriate data type is important to determine the total number or value to be stored. • For example, using Byte data type for Age variable is more than enough to store value from 0 until 255 (enough to reach the maximum human age). • However, Integer data type needs to be used for storing value from the Yearly Salary variable because the total maximum Yearly Salary can have a higher value (Integer can store value up to 2,147,483,647).

  5. Variables • A Variable is a temporary storage location. It holds certain values for a certain period of time and the value can be changed. For example, you can store value number 40 in the variable. This number 40 will not change until there is another value to be inserted in the same variable. In reality, the variable concept has existed in our daily life. For example: • Name = “Ibrahim Zahir” • Address = “Velaanaage” • Qualification = “Degree” • It can be seen that the statement used can be divided into three elements, which are variable, operator and value. For example; Name : variable = : operator “Ali Zahir" : value

  6. Naming the variables • The variable name is the name that represents the variable but is dependanton the following conditions: 1. It cannot begin with numbers, example: 9num. 2. It cannot have spaces between words, example: max value. 3. It cannot contain hyphen marks, example: num-max. 4. Can contain underscore in the variable name, example: num_max. 5. The variable name can contain numbers, example: Value1_1. • Any variable to be used needs to be declared first. The choice of variable name is not a big issue, but you should think about variable names that are appropriate with the task at hand

  7. Variable declaration • Variable syntax in Visual Basic is declared in the following format: • The words that are in Bold represent key terms in Visual Basic. Dim is the key term placed at the beginning of sentences to declare a variable. • Examples of variables; Dim Age as integer • The example shows a space has been assigned to a variable named Age as an Integer in a program. You can also combine the declaration with a few variables by using just one statement. Look at the following: Dim Total as Float, Sale as Currency

  8. Visual Basic provides an Option Explicit instruction to be placed at the top most of any program code. The purpose of this instruction is to ensure that all variables to be used are declared first. After a name is given and declared, values can be assigned in the variables. • Variable name = value

  9. Arithmetic

  10. Arithmetic priority order

  11. Adding new forms • Form can be added in projects to perform other tasks within the same form or system. In this example, you will add one form to perform multiplication of numbers. (a) Open prjDemo. (b) Click on Project menu. (c) Click Add Form, choose new form. (d) Place objects (e) Properties are shown in the properties window that need to be changed for Objects which are used (f) Next, save this form under the name frmCalculator in the same directory as frmDemoand prjDemo

  12. (g) Do not run this form yet! (h) This form will multiply two numbers which are numbers entered in txtnum1 box and txtnum2 box. (i) Results are displayed in the textResult box. The user enters txtnum1 followed by txtnum2. (j) The user cannot edit the textResult box. This is made possible by modifying the space ‘Enable’ to False in the properties window. (k) When the user has entered two values, he/she will click the multiply button to obtain multiply results. (l) Return button enables the multiply form to be returned to Demo form. (m) Refer to Topic 8 to design the Demo form

  13. The following Figure 9.2 is a simple program code that is written for the btnMultiplyby using the Click ( ) event: Private Sub btnMultiply_Click ( ) txtResult.Text = txtnum1.Text * textnum2.Text

  14. Controlling form • When you run the program, you will find that frmCalculator is not displayed. Instead, frmDemo will appear. Is this because a mistake has occurred while saving frmCalculator? To answer this question, follow the next steps. (a) Click on Project menu in the VB6 menu bar. (b) In the project menu, click on prjDemo Properties (if you have not changed the project name yet). (c) In General tab, click on the pull-down box for Startup Object. (d) Click on frm Calculator (e) You can change the project name in the Project Name text box. (f) Click on the OK button. Run the program and you will find that the multiply form will appear

  15. At this time, you need to use one form only although you have two forms in the Project Demo. How can you use both forms? • Follow these steps: (a) Stop running the program. (b) Open Demo form. (c) Add one more command button in the Demo form. (d) Name this button btnMultiply. (e) Set the Caption as &Multiply, in order for Multiply to be displayed. (f) At this time, Demo form should have the objects

  16. (g) Double click btnMultiply and type the instructions btnMultiply_Click ( ) function, • Private Sub btnMultiply_Click ( ) • MousePointer = vbHourglass • Unload me • Call frmCalculator.Show • Mousepointer = 0 • End Sub

  17. Instruction used and exlanation

  18. Private Sub btnReturn_Click ( ) • Unload Me • Call frmDemo.Show • End Sub

  19. To make frmDemo the main form when running the program, follow the next steps: (a) Click on project menu in the VB menu bar. (b) Click PrjDemo Properties (or Project1 Properties, if you have not changed the project name). (i) Set the frmDemo as Startup Object. (ii) Run the project

  20. MDI form • In Demo and Multiply form examples, the interface displayed is placed on the existing windows interface. MDI (Multiple Document Interface) allows the programmer to run form in a VB environment only. To use MDI: (a) Click project menu on the menu bar. (b) Choose Add MDI Form. (c) Click new MDI Form. (d) In the properties space, change the MDI name to MDIDemo. (e) Fill in ‘System Demo - Main Interface’ in the Caption space. (f) Click on Menu Editor, on the VB toolbar

  21. (g) Displayed will be a menu similar

  22. (h) Type & Fail in the Caption space. (i) Type mnuFail in the Name space. (j) Type &keluar in the Caption space. (k) Type mnuKeluar in the Name space. (l) Click on the arrow button (m) You will find that the menu displayed is similar to the section of the menu

  23. (n) Click OK. (o) You will find a menu on the MDIDemoform (p) Click on File and then click Exit. Insert the following program code. Private Sub mnuKeluar_click ( ) End End Sub (q) Run the program.

  24. ASCII code • ASCII code is a code used to represent alphabets and letters in the computer. • Tips • Make sure that all MDI forms and the forms that you have developed are placed in the same directory. This is very important. • To start MDI form as your start up form, make it the Startup Object. The following are additional codes used in the program.

  25. 1. Double click on frmCalculator. Type in KeyPreview = True in the Sub Form_Load method Private Sub Form_Load ( ) KeyPreview = True End Sub • 2. Create a method Form_KeyPress (KeyAscii As Integer) and type the statement Private Sub Form_Keypress (KeyAscii As Integer) TxtOutput.Text = txtOutput.Text + Chr$(KeyAscii) End Sub

  26. 3. The following Table is a list of ASCII keys that you can use to control your program

  27. Summary • Overall, this topic is an introduction to Visual Basic programming. • Just like other programs, Visual Basic has its own data. • The most important type of data is variables. Variable in Visual Basic can receive different types of data such as integers, float, string, char, etc. • Choosing the appropriate data type is important to determine the total number or value to be stored. • A Variable is a temporary storage location. It holds certain values for a certain period of time and the value can be changed. • In reality, the variable concept has existed in our daily life. The variable name is the name that represents the variable

More Related