locked
I want to make a program that estimates a tractor size, but im having a problem on how to program buttons. plss help. RRS feed

  • Question

  • First graphic window will show 2 buttons, primary and secondary. Then when u click the primary button, another graphic window would appear, and there you will input all the data to compute for tractor HP, but the program keeps on failing. Attempted to divide by zero error always pops out when I press the calculate button. plss help.

    Sample Import: XPN746

    • Edited by Argielyn Wednesday, April 11, 2018 6:13 PM
    Wednesday, April 11, 2018 4:12 PM

Answers

  • Hi,

    There are a couple issues I see first.

    1] The actual crash is due to a division by zero.

    FieldCapcity= area/time

    This line is called after getting time from a textbox that is empty and the empty default value is zero

    time=Controls.GetTextBoxText(Timebox)

    2] More importantly you are not using button events right.  I suggest some simple test programs first to understand how they work.

    For example:

    Controls.ButtonClicked= Forprimarybutton
    Controls.ButtonClicked= Forsecondarybutton
    Controls.ButtonClicked= anybutton

    In above the button click subroutine is set 3 times, each time replacing the subroutine call, so the first 2 cannot occur since they are overwritten by by following assignment of the button click event subroutine.

    The methodology should be:

    1] Create all the buttons

    2] Set a single event subroutine for any/all button presses

    3] Inside this single subroutine handle any button press depending on Controls.LastClickedButton property to say which button it was, or just set a flag handled in a 'game loop'

    4] Beyond this you shouldn't do too much inside the event subroutine (especially other GUI stuff) - use flags (set variables) that are handled inside a main UI thread (User Interface) game loop.

    The point 4 is a bit confusing when first encountered, here is a bit more info.

    https://social.technet.microsoft.com/wiki/contents/articles/15060.small-basic-event-basics.aspx

    social.technet.microsoft.com/wiki/contents/articles/20865.small-basic-dynamic-graphics.aspx

    https://social.technet.microsoft.com/wiki/contents/articles/22264.small-basic-threading.aspx

    So I suggest a very simple program to get started using buttons and events, perhaps starting like this:

    Initialise()
    
    'Game loop - keep running waiting For user To do something
    While ("True")
      If (clicked) Then 'Handle click
        clicked = "False" 'We handled it
        button = Controls.LastClickedButton
        For i = 1 To 5
          If (button = buttons[i]) Then
            'Do something
            GraphicsWindow.ShowMessage("Button "+button+" was clicked","Information")
          EndIf
        EndFor  
      EndIf  
      
      Program.Delay(20) 'Don't mash cpu unnecessarily
    EndWhile
    
    Sub Initialise
      'Create buttons
      For i = 1 To 5
        buttons[i] = Controls.AddButton("Button "+i,30,30*i)
      EndFor
      'Set button press event
      Controls.ButtonClicked = OnButtonClicked
    EndSub
    
    Sub OnButtonClicked
      'Flag a button was pressed
      clicked = "True"
    EndSub
    Hope this makes some sense, when considered with the links.

    • Edited by litdev Wednesday, April 11, 2018 9:26 PM
    • Marked as answer by Argielyn Friday, April 13, 2018 5:01 AM
    Wednesday, April 11, 2018 9:23 PM

All replies

  • If you want help, you will have to give the relevant part of the program here, or publish the whole program through ther "Publish" button. Then it will be possible to look for errors.

    Jan [ WhTurner ] The Netherlands

    Wednesday, April 11, 2018 5:19 PM
    Answerer
  • Sample Import: XPN746

    Kindly see if ytou could help me fix the program. tnnxxx

    Wednesday, April 11, 2018 6:12 PM
  • Hi,

    There are a couple issues I see first.

    1] The actual crash is due to a division by zero.

    FieldCapcity= area/time

    This line is called after getting time from a textbox that is empty and the empty default value is zero

    time=Controls.GetTextBoxText(Timebox)

    2] More importantly you are not using button events right.  I suggest some simple test programs first to understand how they work.

    For example:

    Controls.ButtonClicked= Forprimarybutton
    Controls.ButtonClicked= Forsecondarybutton
    Controls.ButtonClicked= anybutton

    In above the button click subroutine is set 3 times, each time replacing the subroutine call, so the first 2 cannot occur since they are overwritten by by following assignment of the button click event subroutine.

    The methodology should be:

    1] Create all the buttons

    2] Set a single event subroutine for any/all button presses

    3] Inside this single subroutine handle any button press depending on Controls.LastClickedButton property to say which button it was, or just set a flag handled in a 'game loop'

    4] Beyond this you shouldn't do too much inside the event subroutine (especially other GUI stuff) - use flags (set variables) that are handled inside a main UI thread (User Interface) game loop.

    The point 4 is a bit confusing when first encountered, here is a bit more info.

    https://social.technet.microsoft.com/wiki/contents/articles/15060.small-basic-event-basics.aspx

    social.technet.microsoft.com/wiki/contents/articles/20865.small-basic-dynamic-graphics.aspx

    https://social.technet.microsoft.com/wiki/contents/articles/22264.small-basic-threading.aspx

    So I suggest a very simple program to get started using buttons and events, perhaps starting like this:

    Initialise()
    
    'Game loop - keep running waiting For user To do something
    While ("True")
      If (clicked) Then 'Handle click
        clicked = "False" 'We handled it
        button = Controls.LastClickedButton
        For i = 1 To 5
          If (button = buttons[i]) Then
            'Do something
            GraphicsWindow.ShowMessage("Button "+button+" was clicked","Information")
          EndIf
        EndFor  
      EndIf  
      
      Program.Delay(20) 'Don't mash cpu unnecessarily
    EndWhile
    
    Sub Initialise
      'Create buttons
      For i = 1 To 5
        buttons[i] = Controls.AddButton("Button "+i,30,30*i)
      EndFor
      'Set button press event
      Controls.ButtonClicked = OnButtonClicked
    EndSub
    
    Sub OnButtonClicked
      'Flag a button was pressed
      clicked = "True"
    EndSub
    Hope this makes some sense, when considered with the links.

    • Edited by litdev Wednesday, April 11, 2018 9:26 PM
    • Marked as answer by Argielyn Friday, April 13, 2018 5:01 AM
    Wednesday, April 11, 2018 9:23 PM