IAN LANG ELECTRONICS

When I was at junior school in the late 1970s all the rich kids had a pocket calculator. They could add, subtract, divide and multiply and had a memory on them that you could save, recall and clear. They cost about five pounds, which, when you think that a very good wage for a working man was £120 a week, was a significant bit of wad to be spending. Now a calculator of equal power is given away free by sales reps.

When I got to Technical College in the 1980s a pre-requisite was that you'd got to have a scientific calculator. As an apprentice I earned £48 per week and had to spend thirty-five on a Casio fx- 911. I've still got it. It does not owe me a penny because I've had thirty years use out of it and both my sisters used it for their O level maths as well. I retired it this year from going outdoors because I don't want it damaged, and bought another scientific calculator from WH Smith-  £5:99

Such is progress. So in this, the first of our tutorials, we are going to make a facsimile of the kind of cheapo calculator that doesn't do much that reps give away in the hope you'll buy from them. It's a psychological thing, they believe if they give you something you'll feel the need to reciprocate. Ha, reps! because we don't need your rubbishy calculator. We're going to make one of our own! Here's a pic of what mine looks like:

 

 

Visual Basic 2010

A Simple Calculator

muchocheapo

It consists of twenty-one buttons, sixteen big ones and five little ones, two rich text boxes, and a label to decorate the top of it with in slanty writing.

 

It adds, or subtracts, or divides, or multiplies, two numbers together by clicking on the numbers and the mathematical operation buttons. Button M lets you put whatever is in the display into the memory, MR lets you recall it to the display, C lets you clear a digit you put in by mistake, and CE resets the whole thing. Two textboxes?  You can't see the second on that picture because it's in the top right hand corner of the display. It puts a little M on the screen if anything other than 0 is in the memory.

 

It's a nice little project that does something useful to kick off with, and from it we can learn about variables, select case statements, IF...THEN statements and there might be an unfocussed ramble or two on the way, possibly with a chance of a smutty double-entendre in the manner of a carry-on film here and there. You never know when it'll pop up............

So, if you've made the interface as above (or to your own designs) you'll need to know how to set the properties. The buttons are easy, just alter the back colours (BackColor) to whatever you'd like it be by clicking on the BackColor property in the properties window, and the text  by typing whatever you want as the text in the window. Confusingly I used the percentage (%) symbol as the division, and you'll find it's diffiicult to reproduce the decimal point. Unless of course you type in n  (note the lower case)  and change the font to Marlett. The decoration on top is a label and has the forecolor red and the font as Balloonist. Set both text boxes to read only.

 

Now we need to look at the code that works it. The first thing you'd do is to type in a number, and so that's the way on we'll go with this too.

 

All of the number button codes work the same way; all they do is count the string then append to it until the string has eight characters in length. Here's the code behind button 1:

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       REM this is the 1 button

       If equalsflag = 1 Then

           equalsflag = 0

           Display.Text = "0"

       End If

       If Display.Text = "0" Then

           Display.Text = ""

       End If

       If Len(Display.Text) < 8 Then

           Display.Text = Display.Text + "1"

 

       End If

 

   End Sub

 

Let's chunk it down.

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

The above is the header which VB puts in automatically. Private Sub means this is the function containing the code that pertains to this object; the object in this case being Button1. Thereafter is the evnt that triggers it: Click. Everything thereafter is the gubbins that the system needs to work the applicationin itself and Windows. There's no user-defined code in there, and the matching compliment at the very end of the subroutine  is End Sub. Visual Basic puts this in automatically too and the user-defined code is put inbetween. Here's the first line:

 

       REM this is the 1 button

 

Staements beginning with REM are remark statements. They do absolutely nothing. However they are used by programmers to mark spots of interest in a program or to denote what a block or line of code does. In this case it's telling us that this is the code for the button that puts a 1 in the display when you click it. The next line starts the real code with a conditional:

 

       If equalsflag = 1 Then

           equalsflag = 0

           Display.Text = "0"

       End If

 

IF.....THEN statements are possibly the most important things you can have in programming as they allow a program to branch different ways for different occurences. There are other forms of conditional which we will meet soon, but the IF.....THEN is the simplest and often best solution to giving your programs intelligent choices.

You start with an argument. Not in the sense that you might have with your wife/girlfriend/significant other though. The argument here sets the conditional and it is equalsflag = 1  and if equalsflag does in fact equal 1 then the code between If and End If gets executed, if equalsflag does not equal 1 then the code doesn't get executed. So the condition can be either true (equalsflag=1) or false (equalsflag=anything other than 1) and in technical terms if equalsflag = 1 then the condition is said to be satisfied, if not the condition unsatisfied. The variable equalsflag is a check variable and we'll meet it later; it's set to 1 when you click the equals button and in no other case. The code that runs if the condition is satisfied is:

 

           equalsflag = 0

 

which resets the flag, and

 

        Display.Text = "0"

 

which puts a 0 in the display, the rich text box.

 

The next line works in tandem with the above and also with some of the other functions, and what looks like an unnecessary step is actually there to clear the box:

           

       If Display.Text = "0" Then

           Display.Text = ""

       End If

 

Those three lines above get rid of the 0 we put in the box and clear it down. But they also ensure that if anything else is there, it stays there to be appended, as the code can run only if the conditional is satisfied. Next we count how many characters are already in the box and make sure it's eight or less:

 

       If Len(Display.Text) < 8 Then

 

Len is a keyword in VB that means "count length of string". Of course you then have to specify the string you want to count. Up above, we specified the text in the rich text box called Display (Display.text) and said if it was less than 8,  (<8) do the code under the conditional. That code is:

 

           Display.Text = Display.Text + "1"

 

In which we append to the text in Display another character, this time being 1. We always have to conclude if statements so the system knows where conditional code ends and here it is:

 

       End If

 

Remember how I banged on earlier about the complimentary to the header being End Sub? Well, here it is:

 

   End Sub

 

And the system knows there's no more code for this routine and exits when it sees that.

 

All the number buttons work just the same way. Here's the one for the 0 button:

 

       REM this is the 0 button

       If equalsflag = 1 Then

           equalsflag = 0

           Display.Text = "0"

       End If

       If Display.Text = "0" Then

           Display.Text = ""

       End If

       If Len(Display.Text) < 8 Then

           Display.Text = Display.Text + "0"

 

       End If

   End Sub

 

And you see the only difference is that we append 0 to the text instead of 1. For the 2 button 2 is appended, for the 3 button 3 is appended and so on. The point button is a little different but not much:

 

REM this is the point button

       If equalsflag = 1 Then

           equalsflag = 0

           Display.Text = "0"

       End If

       If Len(Display.Text) < 8 Then

           Display.Text = Display.Text + "."

 

       End If

 

The only difference being we don't clear off that leading 0 because if you press the point button before any other digit you want nought - point- something and so the zero stays in the text.

 

Once you've typed in a number you want to add to, multiply with, subtract from or divide by another number, and so you need to press an operation key. Over the page we look at this and how they work.

More > Go Back

The code for this project, which can be opened as a text file, is here:

simple calculator