Name: S2 Chapter 1 ( ) Chapter 1 / Ideas of Programming / P.1 Ideas of Programming 1.1 What is a program? In S1, you should have learned what a program is. A program is a set of instructions that tells a computer what to do. In order a computer to do a job for us, the instructions in the program must be: 1. correct 2. in the proper order; and 3. written in a suitable programming language. Is a program a computer hardware or software? ________________ 1.2 How Programming Works When a programmer writes a program, he/she is telling the computer how to complete the job. 1.3 What is A Programming Language? People express themselves using a language that has many words. But the computer can only understand machine language. It use a simple language that consists of only 1s and 0s, with a 1 meaning "on" and a 0 meaning "off." Trying to talk to a computer in its own language would be like trying to talk to your friends by using Morse code—it can be done, but it is difficult. A programming language acts as a translator between you and the computer. Rather than learning the computer's native language (known as machine language), you can use a programming language to instruct the computer in a way that is easier to learn and understand. A specialized program known as a compiler takes the instructions written in the programming language and converts them to machine language. This means that as a Visual Basic programmer, you do not have to understand what the computer is doing or how it does it. You just have to understand how the Visual Basic programming language works. Chapter 1 / Ideas of Programming / P.2 1.4 Idea of Translators Source program which is written by human in certain programming Object program Translator language which is machine language Execution that the computer understands 1.5 Examples of Programming languages A BASIC (Beginner’s All-purpose Symbolic Instruction Code) BASIC is designed to provide beginners with an easy-to-learn programming language. B Logo We can use Logo to draw attractive graphics. It is usually used to teach programming in schools REPEAT 4 [FD20 RT 90] C Pascal Pascal is designed mainly as a tool for teaching programming concepts and good programming style. program HelloSchool(input,output); var name: string; begin name := ‘Tang Hin’; writeln(‘Hello , ’, name) end. D Visual Basic Visual Basic is similar to BASIC in its instructions. It uses graphics tools to construct programs that can be run in the Windows environment. Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click textbox1.text = "Hello" End Sub End Class Name: Chapter 2 S2 ( ) Chapter 2 / Start to Program! / P.1 Start to Program! 2.1 Download VB 2010 A tutorial web site on how to download and install: http://pcnoproblem.twbbs.org/vb2010-express/ Microsoft’s Official Site to download VB2010: http://www.microsoft.com/express/Downloads/ Remember to choose the English version to download. 2.2 Create and Save a new project 1. Click “New Project” 2. Choose “Windows Forms Application” Chapter 2 / Start to Program! / P.2 Form Designer Toolbox:- Solution Common Explorer Controls Properties Window When some of the above boxes disappear, you can click the buttons to show them. In the above screen, object Form1 is being edited. To save the project, it is good to choose “Save All”. For the first time you click “Save All”, your are required to choose a folder for storing your project. In the above example, all files in “E:\VB_Projects\WindowsApplication1” are necessary! Chapter 2 / Start to Program! / P.3 Find “Button” in Toolbox, and then put it into the form. You can then adjust the properties of the button and the form at Design Time. Adjust the size of the Adjust the size of the form. button. (Called Button1) Change the Text property to “Go.” Double click the button object, you should see that some VB codes appear. It means when Button1 is clicked, do the procedure. The event triggered (觸發) is “Button1.Click”. Public Class Form1 Private Sub Button1_Click(...) Handles Button1.Click MsgBox("Hello") End Sub Insert this line of code End Class 2.3 Open an existing project 1. 2. Click “Open Project” Browse into the folder and you should see a “Solution File”. Choose this file and then click “Open”. Chapter 2 / Start to Program! / P.4 2.4 Making a simple web browser Design the following form by putting some controls on it. Button1 TextBox1 WebBrowser1 Add the following code: Public Class Form1 Private Sub Button1_Click(...) Handles Button1.Click WebBrowser1.Navigate(TextBox1.Text) End Sub End Class To run the program click this “Play” button Question 1: What is the event triggered in the above code? _____________________________ Question 2: What happens when “http://www.google.com” is entered in TextBox1 and then Button1 is clicked? _____________________________________________________________________________ Name: S2 ( ) Chapter 3 Working with Controls Chapter 3 / Working with Controls / P.1 P. 3.1 Using Toolbox to design User Interface To insert a control into your form, you just need to drag the control from the Toolbox and drop it into the form. You ou can reposition and resize it as you like. If the toolbox disappears, click this button to make it show again. Some common controls include: Textbox Label Button PictureBox By adding controls into the form, you can design the User Interface of the program. Note: Such kind of user interface that contains graphics is called Graphical User Interface (GUI). (GUI) i.e. We use VB2010 to create GUI programs. Basically, a Windows Forms Application program consists of the following: Form Control Setting the properties of the form and controls Complete program codes for events that trigger program behavior. behavior 3.2 Changing the Properties The properties of the controls (objects) can be set at design time and run time. time At design time, the Properties Window shows the properties of the selected control. Some commonly used properties are listed on the next page. Program codes can be written to change the properties at run time. The syntax is shown below. Object.Property = New_Value e.g.1 e.g.2 Label1.Text = “Hello!” Button1.BackColor BackColor = Color.Red Chapter 3 / Working with Controls / P.2 Setting Properties at design time using Properties Window. Common Properties of Common Controls: Common Properties of TextBox Common Properties of Label Common Properties of Button Text Text Text TextAlign TextAlign TextAlign Width / Height Width / Height Width / Height Left / Top Left / Top Left / Top Visible Visible Visible Enabled Enabled Enabled Font.size Font.size Font.size ForeColor / BackColor ForeColor / BackColor ForeColor / BackColor (Readonly) AutoSize (PasswordChar) Common Properties of Form: Text Width / Height Left / Top Visible Enabled Font.size ForeColor / BackColor StartPosition / WindowState Chapter 3 / Working with Controls / P.3 P. Chapter 3 / Working with Controls / P.4 Example codes: Private Sub Button1_Click(...) Handles Button1.Click Label1.BackColor = Color.Red Label1.ForeColor = Color.Blue Picturebox1.Visible = False Button1.Enabled = False Button1.Text = “Don’t click me!” Me.Text = “Example” End Sub Note: 1. Spell color in US style. (UK: Colour , US: Color) 2. To specify a color, we need to write Color.XXX, where XXX is the name of the color. 3. When Visible property is set to false, the control (object) is invisible. (disappears) 4. When Enabled property is set to false, the control (object) cannot be used. 5. To specify text property, a pair of double quotation mark (“ … ”) is needed for the string. 6. When the form itself is specified, the word Me is used. Name: Chapter 4 S2 ( ) Chapter 4 / Events / P.1 Events 4.1 Event-driven Programming Language VB2010 is a kind of event-driven programming language. An event refers to an action, such as the user clicking on a button. Writing VB programs takes most of the time writing codes for event procedures. Public Class Form1 Private Sub Form1_Load(...) Handles MyBase.Load Marks the start and end End Sub of all procedures in Private Sub Button1_Click(...) Handles Button1.Click Form1 End Sub End Class Name of event procedures Name of events The outline of the event procedures can be created by Double-clicking on the control. The default event procedure is created. To add event procedures of other events, choose the event in the selection box at the top of code editor. Chapter 4 / Events / P.2 4.2 Common Events for a form Private Sub Form1_Click(...) Handles Me.Click ‘ Triggers when the form is clicked End Sub Private Sub Form1_DoubleClick(...) Handles Me.DoubleClick ‘ Triggers when the form is double-clicked End Sub Private Sub Form1_MouseEnter(...) Handles Me.MouseEnter ‘ Triggers when a mouse moves into the form End Sub Private Sub Form1_Load(...) Handles MyBase.Load ‘ Triggers when the form is loaded End Sub Note: The default event for a form is Load. 4.3 Common Events for common controls Button Label TextBox * * DoubleClick MousrEnter MouseLeave TextChanged * Click (* indicates the default event) Name: S2 Chapter 5 ( ) Chapter 5 / Input and Output / P.1 Input and Output 5.1 Using TextBox to accept input Activity 1: Private Sub Button1_Click(...) Handles Button1.Click TextBox2.Text = TextBox1.Text End Sub TextBox1 Button1 Run the program, enter some words in TextBox1 and click Button1 TextBox2 Describe your observation: Ans: ________________________________________________________________________ Activity 2: Create the following form which contains 2 text boxes, 1 button and 1 label. Complete the program codes for the button. Private Sub ___________________________(...) Handles ___________________________ ______________________________________________________________________________ End Sub Note: You can use the symbol ‘+’ or ‘&’ to join 2 strings. E.g. TextBox1.Text + TextBox1.Text or TextBox1.Text & TextBox1.Text However, ‘&’ is safer. Chapter 5 / Input and Output / P.2 5.2 Using Variables to store data temporarily Type Integer Stores Whole numbers (-32768 to +32767) Single / Double Decimal numbers Boolean Logical values (TRUE or FALSE) String Text information E.g. Dim num as Integer Dim money as Double Dim isWealthy as Boolean Dim studentName as String Activity 3: Create a form that contains 1 text box, 1 button and 1 label. Write the following code 1 2 3 4 Dim S2 As String Private Sub Form1_Load(...) Handles MyBase.Load S2 = "Hello, nice to meet you" End Sub 5 6 7 Private Sub Button1_Click(...) Handles Button1.Click Label1.Text = TextBox1.Text & S2 End Sub Run the program, enter some words into TextBox1 and click Button1 Describe your observation. Note: If line 1 is MOVED to between line 2 and line 3, the variable S2 is only usable within Form1_Load procedure. Therefore, in the procedure Button1_Click, S2 contains nothing (null). So, Label1 will only show the text in TextBox1. Name: S2 Chapter 6 ( ) Chapter 6 / Arithmetic Operators / P.1 Arithmetic Operators 6.1 Common Arithmetic Operators Operator Meaning Example Result + To add two numbers 3+8 11 - To subtract two numbers 10-15 -5 * To multiply two numbers 3*8 24 / To divide two numbers and return the result in decimal number 10/4 2.5 \ To divide two numbers and return the result in integer 10\4 2 To divide two numbers and return the remainder 4 mod 3 1 To find the exponential power 4^2 16 Mod ^ Activity 1: Find out the value of the following: Input Output 3+6 3–6 15/7 15\6 15 mod 6 3*6 2^5 5^2 3 ^ (5-2) 6.2 Operators Precedence Operators Precedence Exponential (^) Highest (Evaluated first) Negation (-) Multiplication (*) and division (/) Integer division (\) Modulo (mod) Addition (+) and subtraction (-) String concatenation (&) Lowest (Evaluated last) Chapter 6 / Arithmetic Operators / P.2 Activity 2: Find out the value of the following: Input Output 2+3*5 2+9/3 1-3+9*2-1 21/3+4/2 3+5/2 (3+5)/2 2^3+1 3+9\2 6.3 Use of VAL function When user input some data in TextBox1, TextBox1.Text is string type in nature, i.e. is NOT a number. Therefore, a function called VAL is needed to convert string type data into number. TextBox1.Text Activity 3: Design a form as shown. Write the following code: 1 2 3 Private Sub Button1_Click(...) Handles Button1.Click TextBox2.Text = TextBox1.Text + TextBox2.Text End Sub Input numbers in the 2 TextBoxes and click the button, what happens? Ans: ________________________________________________________________________ How can you amend the code so that the program works properly? Line: _________ Amended Code: ________________________________________________ Name: S2 Chapter 7 ( ) Chapter 7 / Flow Control(1) / P.1 Flow Control(1) 6.1 Decision Making A logical expression can give Boolean result, i.e. the result can be True or False. 6.2 Relational Operators Fill in the missing column ‘Opposite’. The first one has been done for you. Relational Operators Meaning Example Result = Equal to 9 = 11 False > Greater than 11 > 9 True < Less than 11 < 9 False >= Greater or equal 15 >= 15 True <= Less or equal 9 <= 15 True <> Not equal 9 <> 9 False Opposite Conversion to program codes Test 1. Is Peter(PHeight) taller than Mary(MHeight) 2. Is Linda’s age (LAge) not equal to May’s age 3. The passing mark is 50. Has Gigi(GMark) failed the test? 4. It is free to travel by MTR for a person with height 1m or below. Is it free for Kitty(KHeight) to travel by MTR? 5. Is X an even number? Conditions with operators <> Chapter 6 / Arithmetic Operators / P.2 6.3 Conditional Statements VB Syntax: E.g. If condition Then If Val(Text1.Text)=1 Then Text2.Text= “It is one!” Instructions1 End if Instructions2 End If VB Syntax: E.g. If condition Then Private Button1_Click(...) Handles Button1.Click Instructions1 Instructions2 …… firstnum = Label1.Text secondnum = Label2.Text total = firstnum + secondnum Else Instructions3 If total = Val(TextBox1.Text) Then Instructions4 Label3. Text = “Correct! Well Done”Else …… Label3. Text = “Wrong! Try again” End If End If End sub