# Secondary ICT: Using code to create events

The intention of this session is to move the students on to using code to create events, in this case programming buttons to make a calculator, although once the basic principles are mastered, there are other opportunities which the students could investigate.

Arrangements needed prior to the lesson

1. Students need to be aware of the meaning of terms: events e.g. Click and methods, the code that enables the program carry out its design function

2. Students need to be introduced to some data types. The only data types used in the basic approach to this exercise are integer and single (to accommodate decimal fractions). I mention these and a couple of others in the student text below.

Suggested third lesson

In the previous exercise we found our way around the VB 2010 Express IDE, learnt about some common screen objects and found ways of altering their properties during design and running of the program. This session focuses on writing code rather than designing a nice user interface. We are going to make a calculating machine, but once the method for this is established, there are a number of other purposes which this project can be used to serve.

By the by, what does IDE mean; have you got one?

The Project

To start let’s think of how we can ask the computer to add two numbers together. We shall have to think of the machine in very simple terms. For instance, if the user inputs a number, the computer will need somewhere to store that number while the user enters the second number for the addition. Not only this, if the program is to be regularly used, we shall have to make it possible for the computer to forget a previous addition and start a new one.

Algorithms

You used a “Flow Diagram” in the previous exercise and we are going to need to use one again to work out a suitable way for getting the computer to add two numbers and display the answer. This “suitable way” is called an algorithm.

To start, on paper draw a flow diagram to represent the way you would add two numbers together. Keep it simple, we can expect the computer to come with basic ideas of how maths works such as working in decimal and being able to handle overflow such as carrying a figure into the next column to the left. Here are some flow diagram symbols which you might use:

If you hit the wall and can’t make any more progress, why not compare notes with someone else? Data types and other dodgy characters One of the things you will need to consider is how data is to be used and stored in your program. At the moment you only need to decide whether the data is to be handled by the computer as mathematical numbers or whether the data can be handled as text. For us it seems obvious which is which, but consider a telephone number:

01234567890

Although we commonly talk of this as a number, it’s obvious that starting with 01, this is not intended to be used as a number in the mathematical sense. The number can be stored as text because just like a word it isn’t intended to have a mathematical calculation applied to it; just as no one will ask you for the square root of John! On the other hand, the number of hours worked must be stored in the computer as a number because it is used to calculate the total pay. What about the dodgy characters? Well Rasputin was one; I expect you can think of others.

Here are some non-dodgy data types that you will come across:

• String. This data type is used for a sequence of letters such as a word or name, such as extreme or John
• Char. This is used where you want the program to handle a single letter, such as e or J
• Integer. This is a datatype to use when the program is going to use whole numbers, either positive or negative, for example 7 or 15 or -164
• Single. This is used when the program is required to use fractions such as decimals. The number may be positive or negative, for example 2.5 or -0.16

In the last two data types the absence of a sign (i.e. + or -) is taken as meaning that the number is positive.

The Interface

1. Open VB2010 Express

2. On the Start Page click the item New Project (You can always get back to the Start Page by clicking its icon in View on the menu bar).

3. Choose Windows Forms Application and click OK 4. Use the Toolbox to add three Textboxes and one Button to your form.

5. Now change the Properties of the objects as follows:

Remember that you can use any memorable relevant word in the Name or Text properties, it doesn’t have to be the ones I have chosen above: be independent! That’s the basic Interface completed. There is more you could do such as investigating the colour options, which you can change using Properties, or you could add a picture using a Picture box as in the previous exercise and add a title using a Label

6. In the File menu, click Save All (if you have started fresh today you will need to complete the Save Project box with a suitable filename and location for the file). 7. Remember to Close the project. That’s it! In the next session we will add some methods to the program so that it will respond to an event, in this case, the click of the button.

John Giles