Button Click Event

J4nis05 1 Reputation point
2021-11-16T20:42:19.95+00:00

I'm Playing around with small basic and I'm trying to add two Buttons at the end of the program, but these don't do anything. I currently only know how to do things with If- and while-loops.
This is the code:
ButtonRestart = Controls.AddButton("Restart", 10, 10)
ButtonExit = Controls.AddButton("Exit", 90, 10)
Controls.SetSize(ButtonRestart, 70, 26)
Controls.SetSize(ButtonExit, 70, 26)

  If Controls.LastClickedButton = ButtonRestart Then
    GraphicsWindow.Clear()
    GraphicsWindow.Hide()
    Goto Start
  ElseIf Controls.LastClickedButton = ButtonExit Then
    GraphicsWindow.Hide()
    Program.End()
  EndIf
Developer technologies | Small BASIC
Developer technologies | Small BASIC
A programming language created by Microsoft that serves a stepping stone for beginners from block-based coding languages to more complex text-based languages.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Scout 541 Reputation points
    2021-11-17T11:23:48.993+00:00

    The ButtonClick event should be captured in a subroutine and processed via a variable in the main program.

    150194-buttonloop.jpg

    0 comments No comments

  2. WhTurner 1,616 Reputation points
    2021-11-17T15:15:00.67+00:00

    To make it easier for someone to use your program it is better to publish it as code instead as an image:
    Use the fifth button on the top of the answerform ( marked with 001 010 ) It is then easy to copy.

    GraphicsWindow.Title = "Buttonloop"
    GraphicsWindow.Width = 400
    GraphicsWindow.Height = 200
    
    ON = Controls.AddButton("ON",10,20)
    OFF = Controls.AddButton("OFF",200,20)
    
    Controls.ButtonClicked = buttonclicked
    
    End=0
    While End = 0
      If BC=1 Then
        BC = 0
        lastbutton = Controls.LastClickedButton
        If lastbutton = OFF Then
          End = 1
        ElseIf lastbutton = ON Then
          GraphicsWindow.ShowMessage("You pressed ON","Answer")
        EndIf
      EndIf
    EndWhile
    Program.End()
    
    
    Sub buttonclicked
      BC = 1
    EndSub
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.