Pages

Sunday, May 19, 2013

Multiplication table in Visual Basic 6.0


This program works by inserting a number to a TextBox and when press the "Multiply" button the table multiplication of that number from 1 to 12 is going to appear in the ListBox. In order to do that we're going to user 1 TextBox, 1 Listbox and 1 CommandButton, as shown bellow.



















To make this program works we have to tape the following code in your CommandButton, this case called Command1:


Private Sub Command1_Click()

List1.Clear
Dim n As Integer
n = 0
Do While n < 12
n = n + 1
r = Val(Text1.Text) * n
List1.AddItem (Text1.Text & " x " & n & " = " & r)
Loop
Text1.Text = ""
Text1.SetFocus

End Sub


Now explain the code above:

List1.Clear: This is going to, each time the code runs, clean the Listbox.

Dim  n as Integer: Here we declare the variable "n" which stores Integer values.

n= 0: Here we give to the variable "n" the value 0. This way we initialize this variable which must have a value to use the Do While.

Do While n < 12: Here we say while the variable "n" be less than 12 run the code between the Do While and the Loop.

n= n+1: This code will make you add 1 to the variable "n". This way the variable "n" that had a value of 0 is going to plus 1 while the value of "n" be less than 12.

r = Val(Text.Text) * n: "r" es the constant where the value of the multiplication will be stored, the order represents the operation. 

List1.AddItem (Text1.Text & " x " & n & " = " & r): This part is where literally we say to the program to add to the Listbox the value of the TextBox, the letter "x", the value of the variable "n", the = sign and the value of the variable "r". What we are doing here is concatenating the variables and the multiplication signs. The & sign is used in VB 6.0 to concatenate.

Loop: In case of failure to comply the condition settled in the Do While it retuns back, when the condition is met then the Do While ends. 

Text1.Text = "": This will, each time we press the Multiply button, the TextBox value gets clean. 

Text1.SetFocus: Return the cursor to the Text1. 

If we insert the number 2 to the Textbox then this will be the result.



6 Comments:

  1. i want not like this,,,.. i want in a Tabular form and can be imported to MSExcel App

    ReplyDelete
  2. use full for my project for kids....thank you for simply explanation and add more ecell data to combobox sample

    ReplyDelete