The Times Table Code
The Times Table Code
Start a new project for this. Onto your new Form, place two textboxes and a Button. Set the Text property of Textbox1 to 1, and the Text property of Textbox2 to 10. Set the Text property of the Button to "Go". When the Go button is clicked, the programme will put the numbers from the Textbox into two variables. We'll then put a value into a variable called multiplier. If you're doing the times tables, the format is X multiplied by Y = Z (2 multiplied by 3 = 6) We'll use a For Loop to work out the X part; we'll get the Y part from a multiplier variable. We'll then display the results in something called a Listbox. So add a List Box to your form. It looks like this in the toolbox:
The form you design should look something like this one:
A List box is similar to a Combo Box, in that you have a list of items that the user can select. Here, we're just using it display the results of our programme. We'll add items to the List box with our code, rather than in design time like we did for the Combo box. So, here's the code for the entire programme. Double click your Go button, and add the following: Dim number1 As Integer Dim number2 As Integer Dim multiplier As Integer Dim answer As Integer Dim i As Integer number1 = Val(TextBox1.Text) number2 = Val(TextBox2.Text) multiplier = 2 For i = number1 To number2 answer = i * multiplier ListBox1.Items.Add(i & " Times " & multiplier & " = " & answer)
Next i When you've finished, run the programme and see how it works. You should see this appear in your List box:
Let's run through the code to see how it works.We'll do that on the next page. Click the link below to move on.
As you can see from our last lesson, we've set up five Integer variables - number1, number2, multiplier, answer and i. The next thing we did was to pass whatever is in our two Textboxes straight into the two variable, number1 and number2. The start number goes into textbox one, and the end number goes into textbox2. number1 = Val(TextBox1.Text) number2 = Val(TextBox2.Text) In the next line of code, we set a starting value for the multiplier variable: multiplier = 2 The next thing we had was a For Loop. The For Loop was this: For i = number1 To number2 answer = i * multiplier ListBox1.Items.Add(i & " Times " & multiplier & " = " & answer) Next i Remember: the number1 and number2 variables hold our numbers from the Textboxes. We set these to 1 and 10. So our first line of the For Loop is really this:
For i = 1 To 10 We're saying, "Start a For Loop". Whatever is in the variable called number1, make that the starting number for the Loop. Put this value into the variable called i. The end of the Loop will come when the variable called i has the value 10. Stop looping when you reach this value. The next part of the code reads this: answer = i * multiplier This means, Put into the variable called answer the following sum: whatever is in the variable called imultiplied by whatever is in the variable called multiplier.
No more reading these lessons online - get the eBook here! Finally, a word about the line that displays your text in the list box. It was this: ListBox1.Items.Add(i & " Times " & multiplier & " = " & answer) To add items to a list box with code, first you type the name of your list box: ListBox1 Type a full stop and a drop down list will appear. Select Items from the list. ListBox1.Items Type another full stop and again a drop down list will appear. Select the Add Method ListBox1.Items.Add This method, not surprisingly, lets you add items to your list box. Whatever you want to add goes between a pair of round brackets: ListBox1.Items.Add( ) In between the round brackets, we have this for our code: i & " Times " & multiplier & " = " & answer It might be a bit long, but there are 5 parts to it, all joined together by the concatenate symbol (&):
i " Times " multiplier "=" answer The variable i holds the current value of the For Loop; " Times " is just direct text; multiplier holds the value we're multiplying by (our times table); " = " is again direct text; and answer is the answer to our times table sum. If you want to clear the items from a List box you can do this. At the top of the code, enter this line: ListBox1.Items.Clear() So instead of selecting Add from the final drop down list, select Clear.
Exercise
Add another textbox to your form. This will represent the "times table". So far you have been getting this value directly from the code. For this exercise, get the multiplier value directly from the textbox. Add appropriate labels to all your textboxes.