# Small Basic Button Calculator • ### Question

• Hi, I'm new to programming and I'm trying to make a simple button calculator for addition and subtraction.

How would I make it so that the textbox will allow for more than 1 digit numbers?

I'm looking for a fix similar to this one.

How would I be able to do the operations?

If it helps, this is what I have so far:

```GraphicsWindow.Title = "Calculator"
GraphicsWindow.Show()
GraphicsWindow.Height = "200"
GraphicsWindow.Width = "265"

Equals = Controls.AddButton(" = ", 200,50)

For i = 1 To 9
x = 30*(1+Math.Remainder(i-1,3))
y = 30*(1+Math.Floor((i-1)/3))
buttonValue[i] = i
EndFor

buttonValue = "1"
buttonValue = "2"
buttonValue = "3"
buttonValue = "4"
buttonValue = "5"
buttonValue = "6"
buttonValue = "7"
buttonValue = "8"
buttonValue = "9"
buttonValue = "="
buttonValue = "+"
buttonValue = "-"

Controls.ButtonClicked = OnButtonClicked

Sub OnButtonClicked
buttonClicked = Controls.LastClickedButton
For i = 1 To Array.GetItemCount(button)
If (button[i] = buttonClicked) Then
EndIf
EndFor
If buttonClicked = Plus Then
EndIf
If buttonClicked = Minus Then
EndIf
buttonClicked2 = Controls.LastClickedButton
For i = 1 To Array.GetItemCount(button)
If (button[i] = buttonClicked2) Then
EndIf
EndFor
EndSub
```

Thanks,

Dzu

Tuesday, July 26, 2011 6:51 PM

• To do this we need a few extra variables, the ones I chose were:

value1 - the first value before a calculation

value2 - the second value for the calulation

operator - this is the +,- etc to act on value1 and value2

valueCurrent - this is currently displayed in the textbox

So the user enters numbers that get appended to valueCurrent using the Text.Append method.  The Text.Append is very useful to create a string of numbers, where + would just add them athithmetically.

When a + or - is pressed, the value1 is set to the valueCurrent and the operator is set (+ or -), the valueCurrent is cleared to get the second value.

When the = is pressed, value2 is set to valueCurrent and the sum is performed depending on what the previous operator was.  The result is set to valueCurrent to be displayed.

Also do a search in the SmallBasic forum for 'calculator', there have been several previous ones, eg here.

You may want to add a zero to the numbers.

The modified code is (this is not a final version, but a start):

GraphicsWindow.Title = "Calculator"
GraphicsWindow.Show()
GraphicsWindow.Height = "200"
GraphicsWindow.Width = "265"

Equals = Controls.AddButton(" = ", 200,50)

For i = 1 To 9
x = 30*(1+Math.Remainder(i-1,3))
y = 30*(1+Math.Floor((i-1)/3))
buttonValue[i] = i
EndFor

buttonValue = "1"
buttonValue = "2"
buttonValue = "3"
buttonValue = "4"
buttonValue = "5"
buttonValue = "6"
buttonValue = "7"
buttonValue = "8"
buttonValue = "9"
buttonValue = "="
buttonValue = "+"
buttonValue = "-"

Controls.ButtonClicked = OnButtonClicked

valueCurrent = ""
operator = ""

Sub OnButtonClicked
buttonClicked = Controls.LastClickedButton
For i = 1 To Array.GetItemCount(button)
If (button[i] = buttonClicked) Then
valueCurrent = Text.Append(valueCurrent,buttonValue[i])
EndIf
EndFor
If buttonClicked = Plus Then
value1 = valueCurrent
valueCurrent = ""
operator = "+"
ElseIf buttonClicked = Minus Then
value1 = valueCurrent
valueCurrent = ""
operator = "-"
ElseIf buttonClicked = Equals Then
value2 = valueCurrent
If (operator = "+") Then
valueCurrent = value1+value2
ElseIf (operator = "-") Then
valueCurrent = value1-value2
EndIf
EndIf
EndSub
• Marked as answer by Tuesday, July 26, 2011 9:11 PM
Tuesday, July 26, 2011 8:33 PM

### All replies

• To do this we need a few extra variables, the ones I chose were:

value1 - the first value before a calculation

value2 - the second value for the calulation

operator - this is the +,- etc to act on value1 and value2

valueCurrent - this is currently displayed in the textbox

So the user enters numbers that get appended to valueCurrent using the Text.Append method.  The Text.Append is very useful to create a string of numbers, where + would just add them athithmetically.

When a + or - is pressed, the value1 is set to the valueCurrent and the operator is set (+ or -), the valueCurrent is cleared to get the second value.

When the = is pressed, value2 is set to valueCurrent and the sum is performed depending on what the previous operator was.  The result is set to valueCurrent to be displayed.

Also do a search in the SmallBasic forum for 'calculator', there have been several previous ones, eg here.

You may want to add a zero to the numbers.

The modified code is (this is not a final version, but a start):

GraphicsWindow.Title = "Calculator"
GraphicsWindow.Show()
GraphicsWindow.Height = "200"
GraphicsWindow.Width = "265"

Equals = Controls.AddButton(" = ", 200,50)

For i = 1 To 9
x = 30*(1+Math.Remainder(i-1,3))
y = 30*(1+Math.Floor((i-1)/3))
buttonValue[i] = i
EndFor

buttonValue = "1"
buttonValue = "2"
buttonValue = "3"
buttonValue = "4"
buttonValue = "5"
buttonValue = "6"
buttonValue = "7"
buttonValue = "8"
buttonValue = "9"
buttonValue = "="
buttonValue = "+"
buttonValue = "-"

Controls.ButtonClicked = OnButtonClicked

valueCurrent = ""
operator = ""

Sub OnButtonClicked
buttonClicked = Controls.LastClickedButton
For i = 1 To Array.GetItemCount(button)
If (button[i] = buttonClicked) Then
valueCurrent = Text.Append(valueCurrent,buttonValue[i])
EndIf
EndFor
If buttonClicked = Plus Then
value1 = valueCurrent
valueCurrent = ""
operator = "+"
ElseIf buttonClicked = Minus Then
value1 = valueCurrent
valueCurrent = ""
operator = "-"
ElseIf buttonClicked = Equals Then
value2 = valueCurrent
If (operator = "+") Then
valueCurrent = value1+value2
ElseIf (operator = "-") Then
valueCurrent = value1-value2
EndIf
• 