none
Multiple button events RRS feed

  • Question

  • I wanted to make a tic tac toe or a SImon game but i cant get the program to do something after the firstbutton. If i should use an array how would i go abou doing that. Sample code is appreciated but if possibleexplain what is going on so that i can make more programs.

    PS. I am new to SB

    Saturday, January 15, 2011 4:00 PM

Answers

  • This is a simple 2 button example, hopefully with useful comments about what and why.  If you want more clarification on any point then ask.

    'Create 2 buttons and hold them in the variables button1 and button2
    button1 = Controls.AddButton("Button 1",50,50)
    button2 = Controls.AddButton("Button 2",50,100)
    'Register the button click event and subroutine called when a click occurs
    'This is for ANY button click
    Controls.ButtonClicked = OnButtonClicked

    Sub OnButtonClicked
      'First find out which button was pressed
      button = Controls.LastClickedButton
      'Now check if is any of our buttons created and do an action if it is
      If (button = button1) Then
        GraphicsWindow.ShowMessage("Button 1 pressed", "Information")
      ElseIf (button = button2) Then
        GraphicsWindow.ShowMessage("Button 2 pressed", "Information")
      EndIf
    EndSub

    This is the same functionality using arrays to hold the buttons (6 this time)

    numButton = 6
    For i = 1 To numButton
      button[i] = Controls.AddButton("Button "+i,50,50*i)
    EndFor
    Controls.ButtonClicked = OnButtonClicked

    Sub OnButtonClicked
      lastButton = Controls.LastClickedButton
      For i = 1 To numButton
        If (lastButton = button[i]) Then
          GraphicsWindow.ShowMessage("Button "+i+" pressed", "Information")
        EndIf
      EndFor
    EndSub

    • Proposed as answer by AirWaves Saturday, January 15, 2011 8:24 PM
    • Marked as answer by litdevModerator Friday, January 21, 2011 7:49 PM
    Saturday, January 15, 2011 6:25 PM
    Moderator

All replies

  • This is a simple 2 button example, hopefully with useful comments about what and why.  If you want more clarification on any point then ask.

    'Create 2 buttons and hold them in the variables button1 and button2
    button1 = Controls.AddButton("Button 1",50,50)
    button2 = Controls.AddButton("Button 2",50,100)
    'Register the button click event and subroutine called when a click occurs
    'This is for ANY button click
    Controls.ButtonClicked = OnButtonClicked

    Sub OnButtonClicked
      'First find out which button was pressed
      button = Controls.LastClickedButton
      'Now check if is any of our buttons created and do an action if it is
      If (button = button1) Then
        GraphicsWindow.ShowMessage("Button 1 pressed", "Information")
      ElseIf (button = button2) Then
        GraphicsWindow.ShowMessage("Button 2 pressed", "Information")
      EndIf
    EndSub

    This is the same functionality using arrays to hold the buttons (6 this time)

    numButton = 6
    For i = 1 To numButton
      button[i] = Controls.AddButton("Button "+i,50,50*i)
    EndFor
    Controls.ButtonClicked = OnButtonClicked

    Sub OnButtonClicked
      lastButton = Controls.LastClickedButton
      For i = 1 To numButton
        If (lastButton = button[i]) Then
          GraphicsWindow.ShowMessage("Button "+i+" pressed", "Information")
        EndIf
      EndFor
    EndSub

    • Proposed as answer by AirWaves Saturday, January 15, 2011 8:24 PM
    • Marked as answer by litdevModerator Friday, January 21, 2011 7:49 PM
    Saturday, January 15, 2011 6:25 PM
    Moderator
  • sorry for my last response but with your code excerpt how would i make it so that if button 1 then button 2 is pressed that a different action occurs?
    Monday, January 24, 2011 4:04 PM
  • Do you mean that the order that the buttons are pressed is important?

    if the user presses button1 then presses button2 one thing happens

    if the user presses button2 then presses button1 something else happens

    All of this ispossible, we just need to clear what the logic is before trying to code an example.  Exactly which combination of button presses results in different actions?

    Monday, January 24, 2011 6:30 PM
    Moderator
  • Well if i wanted to do simon then it would need to be in a specific order, or tic tac toe has to have the three buttons in row. Does that make sense.by the way thanks
    Monday, January 24, 2011 6:57 PM
  • Yes that makes sense, just wanted to be sure before trying to answer the wrong question.

    Basically store the buttons pressed in a variable - I chose a string with commas deliminating the button presses.  Enter buttons 2 3 6 4 in that order to win.

    Easiest to explain with an example:

    orderPressed = "" 'Empty string that will hold which buttons were pressed in what order
    orderRequired = "2,3,6,4," 'The required order - the commas deliminate the buttons - note the last comma too

    numButton = 6
    For i = 1 To numButton
      button[i] = Controls.AddButton("Button "+i,50,50*i)
    EndFor
    Controls.ButtonClicked = OnButtonClicked

    Sub OnButtonClicked
      lastButton = Controls.LastClickedButton
      For i = 1 To numButton
        If (lastButton = button[i]) Then
          orderPressed = orderPressed+i+"," ' Next entry - using the commas prevents adding the numbers (eg 2+3 = 5 and 2+','+3 = 2,3) they are added to the character string
        EndIf
      EndFor
      checkOrder()
    EndSub

    Sub checkOrder
      If (orderPressed = orderRequired) Then
        GraphicsWindow.ShowMessage("You Win","Message")
      ElseIf (Text.GetLength(orderPressed) >= Text.GetLength(orderRequired)) Then
        GraphicsWindow.ShowMessage("You Lost","Message")
      EndIf
    EndSub

    Monday, January 24, 2011 7:42 PM
    Moderator
  • Im sorry that im asking so many questions but: I see that to make the array you used this saying(For i = 1 To numButton
      button[i] = Controls.AddButton("Button "+i,50,50*i)
    ) but if you wanted to give each button a place that wasn't related to each other then how would you do it.
    Wednesday, January 26, 2011 4:00 PM
  • If you cannot create the position easily as a function of i, then you could set up an array of positions first that could be indexed by i, such as:

    buttonText[1] = "button1"
    buttonX[1] = 123
    buttonY[1] = 44
    buttonText[2] = "some other button"
    buttonX[2] = 265
    buttonY[2] = 320
    buttonText[3] = "another button"
    buttonX[3] = 478
    buttonY[3] = 52


    numButton = Array.GetItemCount(buttonText)
    For i = 1 To numButton
      button[i] = Controls.AddButton(buttonText[i],buttonX[i],buttonY[i])
    EndFor

    Wednesday, January 26, 2011 6:35 PM
    Moderator