locked
Small Basic Button Calculator RRS feed

  • 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?

    http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/029ce76f-5177-4175-a0f5-82d5b5a03eaf 

    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"
    
    GraphicsWindow.DrawBoundText(1,1,100,"Answer: ")
    Answer = Controls.AddTextBox(100,1)
    
    Minus = Controls.AddButton(" - ",200,100)
    Plus = Controls.AddButton(" + ",240,100)
    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))
     button[i] = Controls.AddButton(i,x,y)
     buttonValue[i] = i
    EndFor
    
    buttonValue[1] = "1"
    buttonValue[2] = "2"
    buttonValue[3] = "3"
    buttonValue[4] = "4"
    buttonValue[5] = "5"
    buttonValue[6] = "6"
    buttonValue[7] = "7"
    buttonValue[8] = "8"
    buttonValue[9] = "9"
    buttonValue[10] = "="
    buttonValue[11] = "+"
    buttonValue[12] = "-"
    
    Controls.ButtonClicked = OnButtonClicked
    
    Sub OnButtonClicked
     buttonClicked = Controls.LastClickedButton
     For i = 1 To Array.GetItemCount(button)
      If (button[i] = buttonClicked) Then
        Controls.SetTextBoxText(Answer,buttonValue[i])
       EndIf
       EndFor
      If buttonClicked = Plus Then
       Controls.SetTextBoxText(Answer,"+")
      EndIf
      If buttonClicked = Minus Then
       Controls.SetTextBoxText(Answer,"-")
      EndIf 
       buttonClicked2 = Controls.LastClickedButton
     For i = 1 To Array.GetItemCount(button)
      If (button[i] = buttonClicked2) Then
        Controls.SetTextBoxText(Answer,buttonValue[i])
       EndIf
       EndFor
     EndSub
     


     

    Thanks,

    Dzu


    Tuesday, July 26, 2011 6:51 PM

Answers

  • 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"

    GraphicsWindow.DrawBoundText(1,1,100,"Answer: ")
    Answer = Controls.AddTextBox(100,1)

    Minus = Controls.AddButton(" - ",200,100)
    Plus = Controls.AddButton(" + ",240,100)
    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))
      button[i] = Controls.AddButton(i,x,y)
      buttonValue[i] = i
    EndFor

    buttonValue[1] = "1"
    buttonValue[2] = "2"
    buttonValue[3] = "3"
    buttonValue[4] = "4"
    buttonValue[5] = "5"
    buttonValue[6] = "6"
    buttonValue[7] = "7"
    buttonValue[8] = "8"
    buttonValue[9] = "9"
    buttonValue[10] = "="
    buttonValue[11] = "+"
    buttonValue[12] = "-"

    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])
          Controls.SetTextBoxText(Answer,valueCurrent)
        EndIf
      EndFor
      If buttonClicked = Plus Then
        value1 = valueCurrent
        valueCurrent = ""
        operator = "+"
        Controls.SetTextBoxText(Answer,valueCurrent)
      ElseIf buttonClicked = Minus Then
        value1 = valueCurrent
        valueCurrent = ""
        operator = "-"
        Controls.SetTextBoxText(Answer,valueCurrent)
      ElseIf buttonClicked = Equals Then
        value2 = valueCurrent
        If (operator = "+") Then
          valueCurrent = value1+value2
        ElseIf (operator = "-") Then
          valueCurrent = value1-value2
        EndIf
        Controls.SetTextBoxText(Answer,valueCurrent)
      EndIf
    EndSub
    • Marked as answer by Dzumeister 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"

    GraphicsWindow.DrawBoundText(1,1,100,"Answer: ")
    Answer = Controls.AddTextBox(100,1)

    Minus = Controls.AddButton(" - ",200,100)
    Plus = Controls.AddButton(" + ",240,100)
    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))
      button[i] = Controls.AddButton(i,x,y)
      buttonValue[i] = i
    EndFor

    buttonValue[1] = "1"
    buttonValue[2] = "2"
    buttonValue[3] = "3"
    buttonValue[4] = "4"
    buttonValue[5] = "5"
    buttonValue[6] = "6"
    buttonValue[7] = "7"
    buttonValue[8] = "8"
    buttonValue[9] = "9"
    buttonValue[10] = "="
    buttonValue[11] = "+"
    buttonValue[12] = "-"

    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])
          Controls.SetTextBoxText(Answer,valueCurrent)
        EndIf
      EndFor
      If buttonClicked = Plus Then
        value1 = valueCurrent
        valueCurrent = ""
        operator = "+"
        Controls.SetTextBoxText(Answer,valueCurrent)
      ElseIf buttonClicked = Minus Then
        value1 = valueCurrent
        valueCurrent = ""
        operator = "-"
        Controls.SetTextBoxText(Answer,valueCurrent)
      ElseIf buttonClicked = Equals Then
        value2 = valueCurrent
        If (operator = "+") Then
          valueCurrent = value1+value2
        ElseIf (operator = "-") Then
          valueCurrent = value1-value2
        EndIf
        Controls.SetTextBoxText(Answer,valueCurrent)
      EndIf
    EndSub
    • Marked as answer by Dzumeister Tuesday, July 26, 2011 9:11 PM
    Tuesday, July 26, 2011 8:33 PM
  • Thanks for the help! :)
    Tuesday, July 26, 2011 9:11 PM