locked
My buttons won't work :( RRS feed

  • Question

  • Again, I have failed and need help.

    I tried to "hook-up" my buttons with events, but I can't get it to work.

    Here is my source code:

    GraphicsWindow.Title = "Random Buttons"
    GW = "20"
    GH = "20"
    GW=GraphicsWindow.Width
    GH=GraphicsWindow.Height
    DW=Desktop.Width
    DH=Desktop.Height
    GraphicsWindow.Top = (DH - GH) / 2
    GraphicsWindow.Left = (DW - GW) / 2

    GraphicsWindow.BrushColor = "Black"
    GraphicsWindow.DrawText(225, 0, "Please click a button below:")
    GraphicsWindow.BrushColor = "Blue"
    P1 = Controls.AddButton("Person 1", 30, 30)
    GraphicsWindow.BrushColor = "Green"
    P2 = Controls.AddButton("Person 2", 260, 30)
    GraphicsWindow.BrushColor = "Red"
    P3 = Controls.AddButton("Person 3", 485, 30)
    Controls.SetSize(P1, 100, 67)
    Controls.SetSize(P2, 100, 67)
    Controls.SetSize(P3, 100, 67)

    Controls.ButtonClicked = OnButtonClicked
    Sub OnButtonClicked
      buttonClicked = Controls.LastClickedButton
      If buttonClicked = "P1" Then
        TextWindow.Write("You clicked Person 1's button!")
      ElseIf buttonClicked = "P2" Then
        TextWindow.Write("You clicked Person 2's button!")
      ElseIf buttonClicked = "P3" Then
        TextWindow.Write("You clicked Person 3's button!")
      EndIf
    EndSub

    I want to expand this soon, but can't until the buttons can act right on the event of being clicked.

    Please help! Any and all appreciated!


    ------------------------------------------------------------------------------------------

    Thursday, August 30, 2012 11:29 PM

Answers

  • What about this?

    GraphicsWindow.Title = "Buttons"
    button1 = Controls.AddButton("Button 1", 1, 1)
    button2 = Controls.AddButton("Button 2", 80, 1)
    Controls.ButtonClicked = ButtonClick
    Sub ButtonClick
      pressedButton = Controls.LastClickedButton
      If pressedButton = button1 Then
        GraphicsWindow.ShowMessage("You clicked button 1!", "Button 1")
      EndIf
      If pressedButton = button2 Then
        GraphicsWindow.ShowMessage("You clicked button 2!", "Button 2")
      EndIf
      EndSub

    Good luck! :)


    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

    Thursday, August 30, 2012 11:35 PM
  • You just don't need the quotes! :)

    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

    • Marked as answer by Joman Mied Friday, August 31, 2012 2:31 AM
    Thursday, August 30, 2012 11:35 PM

All replies

  • What about this?

    GraphicsWindow.Title = "Buttons"
    button1 = Controls.AddButton("Button 1", 1, 1)
    button2 = Controls.AddButton("Button 2", 80, 1)
    Controls.ButtonClicked = ButtonClick
    Sub ButtonClick
      pressedButton = Controls.LastClickedButton
      If pressedButton = button1 Then
        GraphicsWindow.ShowMessage("You clicked button 1!", "Button 1")
      EndIf
      If pressedButton = button2 Then
        GraphicsWindow.ShowMessage("You clicked button 2!", "Button 2")
      EndIf
      EndSub

    Good luck! :)


    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

    Thursday, August 30, 2012 11:35 PM
  • You just don't need the quotes! :)

    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

    • Marked as answer by Joman Mied Friday, August 31, 2012 2:31 AM
    Thursday, August 30, 2012 11:35 PM
  • Oh! {smack forehead} That should help! Thank you!

    ------------------------------------------------------------------------------------------


    • Edited by Joman Mied Friday, August 31, 2012 12:21 AM
    Friday, August 31, 2012 12:21 AM
  • Thanks! Do you mind posting it as the answer?

    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

    Friday, August 31, 2012 12:22 AM
  • There, just removed the quotes and it works! I don't use buttons or GraphicsWindow all that often, so I forgot the proper arguments and setup for it.

    Thanks again, Noah!


    ------------------------------------------------------------------------------------------

    Friday, August 31, 2012 12:31 AM
  • Anytime! :)

    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

    Friday, August 31, 2012 12:34 AM
  • Hi Joman!

    As I somewhat uselessly & recently did in this thread, I prefer to use button captions instead of a var containing a button's name.

    So, instead of a direct button = Controls.LastClickedButton, I use button = Controls.GetButtonCaption( Controls.LastClickedButton ).

    This way, I can check a button using the caption displayed in it!

    I even go without assigning any variables to store a button's name!  Just an extra tip!  :-P


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Friday, August 31, 2012 4:28 AM
    Answerer
  • I very recently also just figured out that you could do that as also!


    -Noah J. Buscher "Nothing is Impossible Until Proven Impossible."

    Friday, August 31, 2012 4:39 AM
  • That would make coding faster and easier! Thanks for the tip, GoToLoop!

    ------------------------------------------------------------------------------------------

    Friday, August 31, 2012 6:43 PM