locked
New programmer RRS feed

  • Question

  • I am a newish programmer. I have some background knowledge from one semester of Computer Science in college (I switched to IT). I found Small Basic a few days ago and I really enjoy it. For my fist challenge, I am attempting to create a simple calculator. So far, I have the UI built. Can someone give me hints on getting the user input (button clicks)?

    __________________________________________________________

    'This program will make a calculator in Small Basic and provide and oppurtunity to translat to Visual Studio
    'This program could be made in a console, but will be created in a graphics box
    'SmallCalc

    drawProgramWindow()
    drawPad()
    drawScreens()

    'This is the area for user input, but I don't know how to do it....

    Sub drawPad
     
      buttonWidth = 125
      buttonHeight = 83
      clearButton = Controls.AddButton("CE",50,150)
      divideButton = Controls.AddButton("/",175,150)
      multiplyButton = Controls.AddButton("*",300,150)
      subtractButton = Controls.AddButton("-",425,150)
     
      sevenButton = Controls.AddButton("7",50,233)
      eightButton = Controls.AddButton("8",175,233)
      nineButton = Controls.AddButton("9",300,233)
      plusButton = Controls.AddButton("+",425,233)
     
      fourButton = Controls.AddButton("4",50,316)
      fiveButton = Controls.AddButton("5",175,316)
      sixButton = Controls.AddButton("6",300,316)
     
      oneButton = Controls.AddButton("1",50,399)
      twoButtons = Controls.AddButton("2",175,399)
      threeButton = Controls.AddButton("3",300,399)
      equalButton = Controls.AddButton("=",425,399)
     
      zeroButton = Controls.AddButton("0",50,482)
      dotButton = Controls.AddButton(".",300,482)
     
     
      Controls.SetSize(clearButton,buttonWidth, buttonHeight)
      Controls.SetSize(divideButton,buttonWidth, buttonHeight)
      Controls.SetSize(multiplyButton,buttonWidth, buttonHeight)
      Controls.SetSize(subtractButton,buttonWidth, buttonHeight)
      Controls.SetSize(sevenButton,buttonWidth, buttonHeight)
      Controls.SetSize(eightButton,buttonWidth, buttonHeight)
      Controls.SetSize(nineButton,buttonWidth, buttonHeight)
      Controls.SetSize(plusButton,buttonWidth, buttonHeight + buttonHeight)
      Controls.SetSize(fourButton,buttonWidth, buttonHeight)
      Controls.SetSize(fiveButton,buttonWidth, buttonHeight)
      Controls.SetSize(sixButton,buttonWidth, buttonHeight)
      Controls.SetSize(oneButton,buttonWidth, buttonHeight)
      Controls.SetSize(twoButtons,buttonWidth, buttonHeight)
      Controls.SetSize(threeButton,buttonWidth, buttonHeight)
      Controls.SetSize(zeroButton,buttonWidth + buttonWidth, buttonHeight)
      Controls.SetSize(dotButton,buttonWidth, buttonHeight)
      Controls.SetSize(equalButton,buttonWidth, buttonHeight + buttonHeight)
    EndSub

    Sub drawScreens
      calcScreen = Controls.AddTextBox(50,50)
      Controls.SetSize(calcScreen,300,35)
      calcOperators = Controls.AddTextBox(50,100)
      Controls.SetSize(calcOperators,300,35)
    EndSub

    Sub buttonCaps
    buttonCaption[16] = "/"
    buttonCaption[15] = "*"
    buttonCaption[14] = "-"
    buttonCaption[13] = "+"
    buttonCaption[12] = "="
    buttonCaption[11] = "CE"
    buttonCaption[10] = "."
    buttonCaption[9] = "9"
    buttonCaption[8] = "8"
    buttonCaption[7] = "7"
    buttonCaption[6] = "6"
    buttonCaption[5] = "5"
    buttonCaption[4] = "4"
    buttonCaption[3] = "3"
    buttonCaption[2] = "2"
    buttonCaption[1] = "1"
    buttonCaption[0] = "0"
    EndSub

    Sub drawProgramWindow
      'Create initial calc window without buttons and set global properties
      GraphicsWindow.Title = "Small Calc"
      GraphicsWindow.Width = 600
      GraphicsWindow.Height = 600
      GraphicsWindow.Left = 0.5 * (Desktop.Width - GraphicsWindow.Width)
      GraphicsWindow.Top = 0.5 * (Desktop.Height - GraphicsWindow.Height)
    EndSub

    Friday, November 18, 2016 6:53 PM

Answers

  • ... hints on getting the user input (button clicks)?

    For a first overview with some examples, plz see

    1. Lesson 3.5: The Controls Object for Button Clicks

    2. Lesson 3.4: for Events and Interactivity

    in the Small Basic Curriculum

    e.g.
    btn1 = Controls.AddButton("Demo Button", 10,10)
    ' ...
    ' ...
    
    Controls.ButtonClicked = OnButtonClick
    
    
    Sub OnButtonClick
      lastBtn = Controls.LastClickedButton
      
      If lastBtn = btn1 Then
        GraphicsWindow.ShowMessage("Button"+ lastBtn +" clicked.", "Button Event")
        
        'ElseIf lastBtn = btnXXX Then
        ' ...  
      EndIf
    EndSub

    • Edited by Pappa LapubEditor Friday, November 18, 2016 8:12 PM
    • Proposed as answer by litdev Friday, November 18, 2016 8:21 PM
    • Marked as answer by litdev Friday, November 25, 2016 6:38 PM
    Friday, November 18, 2016 8:01 PM
    Answerer

All replies

  • ... hints on getting the user input (button clicks)?

    For a first overview with some examples, plz see

    1. Lesson 3.5: The Controls Object for Button Clicks

    2. Lesson 3.4: for Events and Interactivity

    in the Small Basic Curriculum

    e.g.
    btn1 = Controls.AddButton("Demo Button", 10,10)
    ' ...
    ' ...
    
    Controls.ButtonClicked = OnButtonClick
    
    
    Sub OnButtonClick
      lastBtn = Controls.LastClickedButton
      
      If lastBtn = btn1 Then
        GraphicsWindow.ShowMessage("Button"+ lastBtn +" clicked.", "Button Event")
        
        'ElseIf lastBtn = btnXXX Then
        ' ...  
      EndIf
    EndSub

    • Edited by Pappa LapubEditor Friday, November 18, 2016 8:12 PM
    • Proposed as answer by litdev Friday, November 18, 2016 8:21 PM
    • Marked as answer by litdev Friday, November 25, 2016 6:38 PM
    Friday, November 18, 2016 8:01 PM
    Answerer
  • For a simple calculator... there are a few methods. Push it all into a char array and then calculate it by determining if the char is a digit or special character. You could also just have a temp variable and *10 the last digit and add the new.

    //SAMPLE
    
    int _a = 0;
    int _b = 0;
    int _temp;
    char _c;
    int _result;
    
    
    button_press(string text)
    {
      _b = Convert.ToInt32(text);
    
      _temp = (_a*10) + _b;
       //display crap
    
    }
    
    //manipulators
    
    spec_press(string text)
    {
      _result = _temp; //toDo add more then 2 numbers.. if _c != null then _result _c _temp...
      _temp = _a = _b = 0;
     // blah convert string to char.
     _c = someChar(text);
    }
    
    //equals is _result _c _temp
    //do it yourself

    Friday, November 18, 2016 8:19 PM