Thursday, September 27, 2012

Small Basic: A Simple Addition Program

Here is a simple Microsoft Small Basic Program that adds two numbers and displays the answer.

'Create a Graphics Window
GraphicsWindow.Show()
'Create caption and text box for the first variable
GraphicsWindow.DrawText(20,20,"Enter First Number")
typedvar1 = Controls.AddTextBox(200,20)
'Create caption and text box for the second variable
GraphicsWindow.DrawText(20,120,"Enter Second Number")
typedvar2 = Controls.AddTextBox(200,120)
'Create caption and text box for the answer
GraphicsWindow.DrawText(20,220,"The answer is")
Answer1 = Controls.AddTextBox(200,220)
'Create a button for adding the two variables
Controls.AddButton("Add", 220,400)
'The answer goes in textbox labeled Answer1
Controls.SetTextBoxText(Answer1, "  ")
'Execute subroutine named buttonsub when add button is clicked
Controls.ButtonClicked = buttonsub
'Subroutine executed when add button is clicked
Sub buttonsub
  'fetch two numbers entered
  var1 = Controls.GetTextBoxText(typedvar1)
  var2 = Controls.GetTextBoxText(typedvar2)
  'Add the two numbers
  var3 = var1 + var2
  'Display the sum in the textbox named answer1
  Controls.SetTextBoxText(Answer1,var3)
EndSub